Example usage for com.google.gson GsonBuilder setPrettyPrinting

List of usage examples for com.google.gson GsonBuilder setPrettyPrinting

Introduction

In this page you can find the example usage for com.google.gson GsonBuilder setPrettyPrinting.

Prototype

public GsonBuilder setPrettyPrinting() 

Source Link

Document

Configures Gson to output Json that fits in a page for pretty printing.

Usage

From source file:nars.io.JSONOutput.java

License:Open Source License

protected void init(boolean pretty) {
    GsonBuilder builder = new GsonBuilder().addSerializationExclusionStrategy(new ExclusionStrategy() {

        @Override//from ww  w .  j  a  v a2  s .co  m
        public boolean shouldSkipField(FieldAttributes fa) {
            //System.out.println(fa.getDeclaredClass() + 
            //" " + fa.getName());
            if (fa.getName().equals("derivationChain")) {
                return true;
            }

            return false;
        }

        @Override
        public boolean shouldSkipClass(Class<?> type) {
            return false;
        }
    })
            //.registerTypeAdapter(Id.class, new IdTypeAdapter())
            //.setDateFormat(DateFormat.LONG)
            //.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE)
            //.setVersion(1.0)                
            .enableComplexMapKeySerialization().serializeNulls().disableHtmlEscaping();

    if (pretty) {
        builder.setPrettyPrinting();
    }
    gson = builder.create();

}

From source file:nars.util.JSONOutput.java

License:Open Source License

protected void init(boolean pretty) {
    GsonBuilder builder = new GsonBuilder().addSerializationExclusionStrategy(new ExclusionStrategy() {

        @Override/*from w  w  w  . j  ava  2s  .  c om*/
        public boolean shouldSkipField(FieldAttributes fa) {
            return fa.getName().equals("derivationChain");
        }

        @Override
        public boolean shouldSkipClass(Class<?> type) {
            return false;
        }
    })
            //.registerTypeAdapter(Id.class, new IdTypeAdapter())
            //.setDateFormat(DateFormat.LONG)
            //.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE)
            //.setVersion(1.0)                
            .enableComplexMapKeySerialization().serializeNulls().disableHtmlEscaping();

    if (pretty) {
        builder.setPrettyPrinting();
    }
    gson = builder.create();

}

From source file:net.famzangl.minecraft.minebot.settings.MinebotSettings.java

License:Open Source License

public static Gson getGson() {
    GsonBuilder gson = new GsonBuilder();
    gson.setPrettyPrinting();
    gson.registerTypeAdapter(BlockSet.class, new BlockSetAdapter());
    gson.registerTypeAdapter(BlockFloatMap.class, new BlockFloatAdapter());
    gson.registerTypeAdapter(ToolRater.class, new ToolRaterAdapter());
    return gson.create();
}

From source file:net.ftb.workers.AuthlibHelper.java

License:Apache License

private static String encode(Map<String, Object> m) {
    try {//w  w  w . j  a va  2s.c o  m
        Gson gson;
        final GsonBuilder builder = new GsonBuilder();
        builder.registerTypeAdapterFactory(new EnumAdaptorFactory());
        builder.registerTypeAdapter(Date.class, new DateAdapter());
        builder.registerTypeAdapter(File.class, new FileAdapter());
        builder.enableComplexMapKeySerialization();
        builder.setPrettyPrinting();
        gson = builder.create();
        return gson.toJson(m);
    } catch (Exception e) {
        Logger.logError("Error encoding Authlib JSON", e);
        return null;
    }

}

From source file:net.minecraft.client.versionRqst.java

public versionRqst() {
    GsonBuilder builder = new GsonBuilder();
    builder.registerTypeAdapterFactory(new LowerCaseEnumTypeAdapterFactory());
    builder.registerTypeAdapter(Date.class, new DateTypeAdapter());
    builder.enableComplexMapKeySerialization();
    builder.setPrettyPrinting();
    this.gson = builder.create();
}

From source file:net.onrc.openvirtex.api.service.handlers.monitoring.GetVirtualTopology.java

License:Apache License

@SuppressWarnings("unchecked")
@Override//from  w  w w. j av  a 2s .  com
public JSONRPC2Response process(final Map<String, Object> params) {
    Map<String, Object> result;
    JSONRPC2Response resp = null;
    Number tid = null;
    try {
        tid = HandlerUtils.<Number>fetchField(MonitoringHandler.TENANT, params, true, null);
        OVXNetwork vnet = OVXMap.getInstance().getVirtualNetwork(tid.intValue());
        // TODO: gson objects can be shared with other methods
        final GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.setPrettyPrinting();
        gsonBuilder.excludeFieldsWithoutExposeAnnotation();
        gsonBuilder.registerTypeAdapter(OVXSwitch.class, new OVXSwitchSerializer());
        gsonBuilder.registerTypeAdapter(OVXPort.class, new OVXPortSerializer());
        /*gsonBuilder.registerTypeAdapter(OVXLink.class,
              new OVXLinkSerializer());*/

        final Gson gson = gsonBuilder.create();
        result = gson.fromJson(gson.toJson(vnet), Map.class);
        resp = new JSONRPC2Response(result, 0);
        return resp;
    } catch (ClassCastException | MissingRequiredField e) {
        resp = new JSONRPC2Response(new JSONRPC2Error(JSONRPC2Error.INVALID_PARAMS.getCode(),
                this.cmdName() + ": Unable to fetch virtual topology : " + e.getMessage()), 0);
    } catch (NetworkMappingException e) {
        resp = new JSONRPC2Response(new JSONRPC2Error(JSONRPC2Error.INVALID_PARAMS.getCode(),
                this.cmdName() + ": Invalid tenantId : " + tid), 0);
    }
    return resp;
}

From source file:net.sf.uadetector.json.internal.data.serializer.Serializers.java

License:Apache License

private Serializers(final EnumSet<SerDeOption> options) {
    Check.notNull(options);/*from  ww  w  . ja v  a  2s  .  co  m*/
    final GsonBuilder gsonBuilder = new GsonBuilder();

    // setup serializers
    gsonBuilder.registerTypeAdapter(Data.class, new DataSerializer());
    gsonBuilder.registerTypeAdapter(Browser.class, new BrowserSerializer());
    gsonBuilder.registerTypeAdapter(BrowserPattern.class, new OrderedPatternSerializer<BrowserPattern>());
    gsonBuilder.registerTypeAdapter(BrowserType.class, new BrowserTypeSerializer());
    gsonBuilder.registerTypeAdapter(OperatingSystem.class, new OperatingSystemSerializer());
    gsonBuilder.registerTypeAdapter(OperatingSystemPattern.class,
            new OrderedPatternSerializer<OperatingSystemPattern>());
    gsonBuilder.registerTypeAdapter(Robot.class, new RobotSerializer());
    gsonBuilder.registerTypeAdapter(Pattern.class, new PatternSerializer());
    gsonBuilder.registerTypeAdapter(Device.class, new DeviceSerializer());
    gsonBuilder.registerTypeAdapter(DevicePattern.class, new OrderedPatternSerializer<DevicePattern>());

    // some settings
    if (options.contains(SerDeOption.PRETTY_PRINTING)) {
        gsonBuilder.setPrettyPrinting();
    }
    gsonBuilder.disableInnerClassSerialization();

    gson = gsonBuilder.create();
}

From source file:net.technic.technicblocks.parser.ModDataParser.java

License:Open Source License

public ModDataParser(InputStream stream, TechnicBlocksCommonProxy proxy) {
    this.proxy = proxy;

    if (stream == null) {
        throw proxy.createParseException("Mod Data json file could not be found.");
    }/*from  w  ww . j av  a2  s . c  o m*/

    GsonBuilder builder = new GsonBuilder();
    builder.setPrettyPrinting();
    gson = builder.create();

    try {
        StringWriter writer = new StringWriter();
        IOUtils.copy(stream, writer);
        String dataText = writer.toString();
        data = gson.fromJson(dataText, ModData.class);

        if (data == null) {
            throw proxy.createParseException("Mod Data json file parsed to a null value.");
        }
    } catch (IOException ex) {
        throw proxy.createParseException("Error loading Mod Data json file.", ex);
    } catch (JsonSyntaxException ex) {
        throw proxy.createParseException("Error parsing the Mod Data json file.", ex);
    } finally {
        try {
            stream.close();
        } catch (IOException ex) {
        }
    }
}

From source file:nz.ac.otago.psyanlab.common.model.util.ModelUtils.java

License:Open Source License

public static Gson getDataReaderWriter() {
    if (mGson == null) {
        GsonBuilder gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation();

        // Type loopArrayType = new TypeToken<LongSparseArray<Loop>>() {
        // }.getType();
        // gson.registerTypeAdapter(loopArrayType,
        // new LongSparseArrayGsonAdapter<Loop>(Loop.class));

        gson.registerTypeAdapter(Operand.class,
                new AbsModelGsonAdapter<Operand>(AbsModelGsonAdapter.NS_MODEL_OPERAND));
        gson.registerTypeAdapter(Generator.class,
                new AbsModelGsonAdapter<Generator>(AbsModelGsonAdapter.NS_MODEL_GENERATOR));
        gson.registerTypeAdapter(Question.class,
                new AbsModelGsonAdapter<Question>(AbsModelGsonAdapter.NS_MODEL_SUBJECT));
        gson.registerTypeAdapter(Prop.class, new AbsModelGsonAdapter<Prop>(AbsModelGsonAdapter.NS_MODEL_PROP));
        gson.registerTypeAdapter(Asset.class,
                new AbsModelGsonAdapter<Asset>(AbsModelGsonAdapter.NS_MODEL_ASSET));
        gson.registerTypeAdapter(Variable.class,
                new AbsModelGsonAdapter<Variable>(AbsModelGsonAdapter.NS_MODEL_VARIABLE));
        gson.registerTypeAdapter(Timer.class,
                new AbsModelGsonAdapter<Timer>(AbsModelGsonAdapter.NS_MODEL_TIMER));
        if (BuildConfig.DEBUG) {
            gson.setPrettyPrinting();
        }// ww w .  j ava 2s. com
        mGson = gson.create();
    }

    return mGson;
}

From source file:och.util.json.GsonUtil.java

License:Apache License

public static Gson gsonWithDateFormat(String dateFormat, boolean prettyPrinting) {
    GsonBuilder builder = new GsonBuilder();
    builder.setDateFormat(dateFormat);//from  www .ja va  2 s  .  c  o m
    if (prettyPrinting)
        builder.setPrettyPrinting();
    return builder.create();
}