Java 类javax.json.JsonWriterFactory 实例源码

项目:tool.lars    文件:DataModelSerializer.java   
/**
 * Convert a POJO into Serialized JSON form.
 *
 * @param o the POJO to serialize
 * @param s the OutputStream to write to..
 * @throws IOException when there are problems creating the JSON.
 */
public static void serializeAsStream(Object o, OutputStream s) throws IOException {
    try {
        JsonStructure builtJsonObject = findFieldsToSerialize(o).mainObject;
        Map<String, Object> config = new HashMap<String, Object>();
        config.put(JsonGenerator.PRETTY_PRINTING, true);
        JsonWriterFactory writerFactory = Json.createWriterFactory(config);
        JsonWriter streamWriter = writerFactory.createWriter(s);
        streamWriter.write(builtJsonObject);
    } catch (IllegalStateException ise) {
        // the reflective attempt to build the object failed.
        throw new IOException("Unable to build JSON for Object", ise);
    } catch (JsonException e) {
        throw new IOException("Unable to build JSON for Object", e);
    }
}
项目:johnzon    文件:JsonGeneratorImplTest.java   
@Test
public void prettySimpleStructure() {
    final JsonWriterFactory writerFactory = Json.createWriterFactory(new HashMap<String, Object>() {
        {
            put(JsonGenerator.PRETTY_PRINTING, true);
        }
    });

    StringWriter buffer = new StringWriter();

    final JsonWriter writer = writerFactory.createWriter(buffer);
    writer.write(Json.createObjectBuilder().add("firstName", "John").build());
    writer.close();

    assertEquals("{\n" + "  \"firstName\":\"John\"\n" + "}", buffer.toString());
}
项目:johnzon    文件:JsonGeneratorImplTest.java   
@Test
public void prettySimpleWriter() {

    final JsonWriterFactory writerFactory = Json.createWriterFactory(new HashMap<String, Object>() {
        {
            put(JsonGenerator.PRETTY_PRINTING, true);
        }
    });

    StringWriter buffer = new StringWriter();

    final JsonReader reader = Json.createReader(new ByteArrayInputStream("{\"firstName\":\"John\"}".getBytes()));
    final JsonWriter writer = writerFactory.createWriter(buffer);
    writer.write(reader.read());
    writer.close();
    reader.close();

    assertEquals("{\n" + "  \"firstName\":\"John\"\n" + "}", buffer.toString());
}
项目:Peking-University-Open-Research-Data-Platform    文件:Util.java   
@Deprecated
static String jsonObject2prettyString(JsonObject jsonObject) {
    Map<String, Boolean> config = new HashMap<>();
    config.put(JsonGenerator.PRETTY_PRINTING, true);
    JsonWriterFactory jsonWriterFactory = Json.createWriterFactory(config);
    StringWriter stringWriter = new StringWriter();
    try (JsonWriter jsonWriter = jsonWriterFactory.createWriter(stringWriter)) {
        jsonWriter.writeObject(jsonObject);
    }
    return stringWriter.toString();
}
项目:Peking-University-Open-Research-Data-Platform    文件:Util.java   
@Deprecated
static String jsonArray2prettyString(JsonArray jsonArray) {
    Map<String, Boolean> config = new HashMap<>();
    config.put(JsonGenerator.PRETTY_PRINTING, true);
    JsonWriterFactory jsonWriterFactory = Json.createWriterFactory(config);

    StringWriter stringWriter = new StringWriter();
    try (JsonWriter jsonWriter = jsonWriterFactory.createWriter(stringWriter)) {
        jsonWriter.writeArray(jsonArray);
    }
    return stringWriter.toString();
}
项目:platform    文件:MarshallerUtil.java   
public static String marshallObject(Object obj) throws JAXBException {
    JsonObjectBuilderResult result = new JsonObjectBuilderResult();
    getJc().createMarshaller().marshal(obj, result);

    Map<String, Object> jsonProperties = new HashMap<String, Object>();
    jsonProperties.put(JsonGenerator.PRETTY_PRINTING, true);
    JsonWriterFactory writerFactory = Json.createWriterFactory(jsonProperties);
    StringWriter strWriter = new StringWriter();
    JsonWriter writer = writerFactory.createWriter(strWriter);
    writer.writeObject(result.getJsonObjectBuilder().build());
    writer.close();
    return strWriter.toString();
}
项目:org.ops4j.ramler    文件:ExampleSpecJsonRenderer.java   
/**
 * Returns a pretty-printed JSON string for the given example instance of the given RAML type.
 *
 * @param type
 *            RAML type declaration
 * @param example
 *            example instance
 * @return pretty-printed JSON string
 */
public String prettyPrint(TypeDeclaration type, ExampleSpec example) {
    JsonValue json = toJsonValue(type, example);

    Map<String, Boolean> config = new HashMap<>();
    config.put(JsonGenerator.PRETTY_PRINTING, true);
    JsonWriterFactory jwf = Json.createWriterFactory(config);
    StringWriter sw = new StringWriter();
    JsonWriter writer = jwf.createWriter(sw);
    if (json instanceof JsonStructure) {
        writer.write((JsonStructure) json);
        return sw.toString();
    }
    else {
        return json.toString();
    }
}
项目:path-optimizer    文件:JsonUtils.java   
private static JsonWriterFactory getPrettyJsonWriterFactory() {
    if (null == FACTORY_INSTANCE) {
        final Map<String, Object> properties = new HashMap<>(1);
        properties.put(JsonGenerator.PRETTY_PRINTING, true);
        FACTORY_INSTANCE = Json.createWriterFactory(properties);
    }
    return FACTORY_INSTANCE;
}
项目:jkool-client-java-api    文件:JKUtils.java   
public static String jsonFormat(JsonStructure json, String... options) {
    StringWriter stringWriter = new StringWriter();
    Map<String, Boolean> config = buildConfig(options);
    JsonWriterFactory writerFactory = Json.createWriterFactory(config);
    JsonWriter jsonWriter = writerFactory.createWriter(stringWriter);

    jsonWriter.write(json);
    jsonWriter.close();
    return stringWriter.toString();
}
项目:tool.lars    文件:SingleFileClient.java   
/**
 * Write the contents of {@link #assets} to {@link #file}.
 * <p>
 * This method ensures that the asset with id {@code n} is always written to the {@code n}th position in the file, using {@code null}s for padding if required.
 */
private synchronized void rewriteFile() throws FileNotFoundException, IOException {
    JsonArrayBuilder jsonToStore = Json.createArrayBuilder();

    // Iterate through the assets in id order
    Map<String, JsonObject> assetMap = assets == null ? Collections.<String, JsonObject> emptyMap() : assets;
    for (int i = 1; i <= idCounter.get(); i++) {
        JsonObject json = assetMap.get(Integer.toString(i));
        if (json == null) {
            jsonToStore.addNull();
        } else {
            jsonToStore.add(json);
        }
    }

    // Write the assets back to the file
    FileOutputStream out = null;
    try {
        Map<String, Object> config = new HashMap<String, Object>();
        config.put(JsonGenerator.PRETTY_PRINTING, true);
        JsonWriterFactory writerFactory = Json.createWriterFactory(config);
        out = new FileOutputStream(file);
        JsonWriter streamWriter = writerFactory.createWriter(out);
        streamWriter.write(jsonToStore.build());
    } finally {
        if (out != null) {
            out.close();
        }
    }
}
项目:lunchy    文件:LunchyProperties.java   
private String prettyPrint(JsonObject json) {
    Map<String, Object> properties = new HashMap<>(1);
    properties.put(JsonGenerator.PRETTY_PRINTING, true);
    JsonWriterFactory writerFactory = Json.createWriterFactory(properties);

    StringWriter sw = new StringWriter();
    try (JsonWriter jsonWriter = writerFactory.createWriter(sw)) {
        jsonWriter.writeObject(json);

    }
    return sw.toString();
}
项目:johnzon    文件:FactoryLocator.java   
@Override
public void contextInitialized(final ServletContextEvent servletContextEvent) {
    final ClassLoader classLoader = servletContextEvent.getServletContext().getClassLoader();

    final JsonReaderFactory reader = newReadFactory();
    READER_FACTORY_BY_LOADER.put(classLoader, reader);
    servletContextEvent.getServletContext().setAttribute(READER_ATTRIBUTE, reader);

    final JsonWriterFactory writer = newWriterFactory();
    WRITER_FACTORY_BY_LOADER.put(classLoader, writer);
    servletContextEvent.getServletContext().setAttribute(WRITER_ATTRIBUTE, reader);
}
项目:johnzon    文件:FactoryLocator.java   
public static JsonWriterFactory writerLocate() {
    ClassLoader loader = Thread.currentThread().getContextClassLoader();
    if (loader == null) {
        loader = FactoryLocator.class.getClassLoader();
    }
    final JsonWriterFactory factory = WRITER_FACTORY_BY_LOADER.get(loader);
    if (factory == null) {
        return newWriterFactory();
    }
    return factory;
}
项目:streamjit    文件:Jsonifiers.java   
/**
 * Prettyprints the given JSON structure.  (All well-formed JSON strings
 * have an array or object at their top level.)
 * @param value a JSON structure
 * @return a pretty-printed JSON string
 */
public static String prettyprint(JsonStructure value) {
    StringWriter string = new StringWriter();
    JsonWriterFactory writerFactory = Json.createWriterFactory(Collections.singletonMap(JsonGenerator.PRETTY_PRINTING, null));
    try (JsonWriter writer = writerFactory.createWriter(string)) {
        writer.write(value);
    }
    return string.toString();
}
项目:jcypher    文件:Util.java   
/**
 * write a JsonObject formatted in a pretty way into a String
 * @param jsonObject
 * @return a String containing the JSON
 */
public static String writePretty(JsonObject jsonObject) {
    JsonWriterFactory factory = createPrettyWriterFactory();
    StringWriter sw = new StringWriter();
    JsonWriter writer = factory.createWriter(sw);
    writer.writeObject(jsonObject);
    String ret = sw.toString();
    writer.close();
    return ret;
}
项目:polygene-java    文件:JavaxJsonFactories.java   
@Override
public JsonWriterFactory writerFactory()
{
    return writerFactory;
}
项目:activemq-artemis    文件:JsonLoader.java   
public static JsonWriterFactory createWriterFactory(Map<String, ?> config) {
   return provider.createWriterFactory(config);
}
项目:johnzon    文件:JsrMessageBodyWriter.java   
public JsrMessageBodyWriter(final JsonWriterFactory factory, final boolean closeStreams) {
    this.factory = factory;
    this.close = closeStreams;
}
项目:johnzon    文件:JsonProviderImpl.java   
@Override
public JsonWriterFactory createWriterFactory(final Map<String, ?> stringMap) {
    return DELEGATE.createWriterFactory(stringMap);
}
项目:johnzon    文件:JsonProviderImpl.java   
@Override
public JsonWriterFactory createWriterFactory(final Map<String, ?> config) {
    return (config == null || config.isEmpty()) ? writerFactory : new JsonWriterFactoryImpl(config);
}
项目:johnzon    文件:FactoryLocator.java   
private static JsonWriterFactory newWriterFactory() {
    return Json.createWriterFactory(Collections.<String, Object>emptyMap());
}
项目:jcypher    文件:Util.java   
private static JsonWriterFactory createPrettyWriterFactory() {
    HashMap<String, Object> prettyConfigMap = new HashMap<String, Object>();
    prettyConfigMap.put(JsonGenerator.PRETTY_PRINTING, Boolean.TRUE);
    JsonWriterFactory prettyWriterFactory = Json.createWriterFactory(prettyConfigMap);
    return prettyWriterFactory;
}
项目:schematica    文件:SchematicaJsonProvider.java   
@Override
public JsonWriterFactory createWriterFactory( Map<String, ?> config ) {
    return defaultProvider.createWriterFactory(config);
}
项目:schematica    文件:Json.java   
/**
 * Creates a writer factory for creating {@link JsonWriter} objects.
 * The factory is configured with the specified map of provider specific
 * configuration properties. Provider implementations should ignore any
 * unsupported configuration properties specified in the map.
 *
 * @param config a map of provider specific properties to configure the
 *               JSON writers. The map may be empty or null
 * @return a JSON writer factory
 */
public static JsonWriterFactory createWriterFactory(Map<String, ?> config) {
    return PROVIDER_INSTANCE.createWriterFactory(config);
}
项目:polygene-java    文件:JavaxJsonFactories.java   
JsonWriterFactory writerFactory();