All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.seedstack.seed.shell.commands.JsonCommand Maven / Gradle / Ivy

The newest version!
/**
 * Copyright (c) 2013-2015 by The SeedStack authors. All rights reserved.
 *
 * This file is part of SeedStack, An enterprise-oriented full development stack.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
package org.seedstack.seed.shell.commands;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.seedstack.seed.core.spi.command.CommandDefinition;
import org.seedstack.seed.core.spi.command.Option;
import org.seedstack.seed.core.spi.command.PrettyCommand;

/**
 * This command serialize the input object to json.
 *
 * @author [email protected]
 */
@CommandDefinition(scope = "", name = "json", description = "Serialize the input object to json")
public class JsonCommand implements PrettyCommand {
    private static final ObjectMapper OBJECT_MAPPER;
    private static final DefaultPrettyPrinter DEFAULT_PRETTY_PRINTER;

    @Option(name = "p", longName = "pretty", description = "Output pretty JSON directly")
    private boolean pretty;

    static {
        DEFAULT_PRETTY_PRINTER = new DefaultPrettyPrinter();
        DEFAULT_PRETTY_PRINTER.indentArraysWith(new DefaultPrettyPrinter.Lf2SpacesIndenter());

        OBJECT_MAPPER = new ObjectMapper();
        OBJECT_MAPPER.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false);
        OBJECT_MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    }

    @Override
    public String execute(Object object) throws JsonProcessingException {
        if (object == null) {
            return null;
        }

        if (pretty) {
            return OBJECT_MAPPER.writer(DEFAULT_PRETTY_PRINTER).writeValueAsString(object);
        } else {
            return OBJECT_MAPPER.writeValueAsString(object);
        }
    }

    @Override
    public String prettify(String object) throws Exception {
        return OBJECT_MAPPER.writer(DEFAULT_PRETTY_PRINTER).writeValueAsString(OBJECT_MAPPER.readValue(object, Object.class));
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy