Java Code Examples for javax.json.spi.JsonProvider#provider()

The following examples show how to use javax.json.spi.JsonProvider#provider() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StringBackend.java    From jaxrs-analyzer with Apache License 2.0 6 votes vote down vote up
private String format(final String json) {
    final JsonProvider provider = JsonProvider.provider();
    final StringWriter out = new StringWriter();
    try (final JsonReader reader = provider.createReader(new StringReader(json));
         final JsonWriter jsonWriter = provider.createWriterFactory(singletonMap(JsonGenerator.PRETTY_PRINTING, true))
                 .createWriter(out)) {

        // jsonWriter.write(reader.readValue()); // bug in RI, can switch to johnzon
        final JsonStructure read = reader.read();
        if (read.getValueType() == JsonValue.ValueType.OBJECT) {
            jsonWriter.writeObject(JsonObject.class.cast(read));
        } else if (read.getValueType() == JsonValue.ValueType.ARRAY) {
            jsonWriter.writeArray(JsonArray.class.cast(read));
        } else { // no reformatting
            return json;
        }
        return out.toString().trim();
    }
}
 
Example 2
Source File: JsonpReader.java    From incubator-batchee with Apache License 2.0 6 votes vote down vote up
@Override
public void open(final Serializable checkpoint) throws Exception {
    final ClassLoader loader = Thread.currentThread().getContextClassLoader();
    final JsonProvider provider = this.provider == null ? JsonProvider.provider() : JsonProvider.class.cast(loader.loadClass(this.provider));
    parser = provider.createParser(new FileInputStream(file));
    reader = new JsonPartialReader(provider, parser);

    if (skipRoot == null || "true".equalsIgnoreCase(skipRoot)) {
        final JsonParser.Event event = parser.next();
        if (event == JsonParser.Event.START_ARRAY) {
            end = JsonParser.Event.END_ARRAY;
        } else {
            end = JsonParser.Event.END_OBJECT;
        }
    }
    super.open(checkpoint);
}
 
Example 3
Source File: ComponentManager.java    From component-runtime with Apache License 2.0 5 votes vote down vote up
private JsonProvider loadJsonProvider() {
    try {
        return new org.apache.johnzon.core.JsonProviderImpl();
    } catch (final RuntimeException re) {
        return JsonProvider.provider();
    }
}
 
Example 4
Source File: ConfigurationLoader.java    From openwebbeans-meecrowave with Apache License 2.0 5 votes vote down vote up
private JsonProvider loadJsonpProvider() {
    try { // prefer johnzon to support comments
        return JsonProvider.class.cast(Thread.currentThread().getContextClassLoader()
                .loadClass("org.apache.johnzon.core.JsonProviderImpl")
                .getConstructor().newInstance());
    } catch (final InvocationTargetException | NoClassDefFoundError | InstantiationException | ClassNotFoundException | NoSuchMethodException | IllegalAccessException err) {
        return JsonProvider.provider();
    }
}
 
Example 5
Source File: JsonSchemaModulesGenerator.java    From jsonix-schema-compiler with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void generate(JsonStructureWriter<T, C> writer) {
	final JsonProvider provider = JsonProvider.provider();
	final JsonBuilderFactory builderFactory = provider.createBuilderFactory(null);
	for (final Module<T, C> module : this.modules.getModules()) {
		if (!module.isEmpty()) {
			for (JsonSchema jsonSchema : module.getJsonSchemas()) {
				final JsonSchemaModuleCompiler<T, C> moduleCompiler = new JsonSchemaModuleCompiler<T, C>(
						builderFactory, modules, module, jsonSchema);
				final JsonSchemaBuilder moduleSchema = moduleCompiler.compile();
				final JsonObject moduleSchemaJsonObject = moduleSchema.build(builderFactory);
				writer.writeJsonStructure(module, moduleSchemaJsonObject, jsonSchema.getFileName());
			}
		}
	}
}
 
Example 6
Source File: JsonBuilderUtilsTest.java    From jsonix-schema-compiler with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Test
public void addsProperty() {
	final JsonProvider provider = JsonProvider.provider();
	final JsonBuilderFactory builderFactory = provider
			.createBuilderFactory(null);
	final JsonObjectBuilder builder = builderFactory.createObjectBuilder();
	JsonBuilderUtils.add(builderFactory, builder, "null", null);
	JsonBuilderUtils.add(builderFactory, builder, "true", true);
	JsonBuilderUtils.add(builderFactory, builder, "string", "string");
	JsonBuilderUtils.add(builderFactory, builder, "char", 'c');
	JsonBuilderUtils.add(builderFactory, builder, "bigInteger",
			BigInteger.TEN);
	JsonBuilderUtils.add(builderFactory, builder, "bigDecimal",
			BigDecimal.valueOf(1111, 2));
	JsonBuilderUtils.add(builderFactory, builder, "float", 22f);
	JsonBuilderUtils.add(builderFactory, builder, "double", 22d);
	JsonBuilderUtils.add(builderFactory, builder, "byte", (byte) 33);
	JsonBuilderUtils.add(builderFactory, builder, "int", (int) 44);
	JsonBuilderUtils.add(builderFactory, builder, "short", (int) 55);
	JsonBuilderUtils.add(builderFactory, builder, "list",
			Arrays.<Object> asList("a", 0xbc, "d"));
	JsonBuilderUtils.add(builderFactory, builder, "array", new Object[] {
			1, "2", 3, false });

	JsonBuilderUtils.add(builderFactory, builder, "map",
			Collections.singletonMap("foo", "bar"));
	// provider.createWriter(System.out).write(builder.build());
}
 
Example 7
Source File: JsonBuilderUtilsTest.java    From jsonix-schema-compiler with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Test
public void addsItem() {
	final JsonProvider provider = JsonProvider.provider();
	final JsonBuilderFactory builderFactory = provider
			.createBuilderFactory(null);
	final JsonArrayBuilder builder = builderFactory.createArrayBuilder();
	JsonBuilderUtils.add(builderFactory, builder, null);
	JsonBuilderUtils.add(builderFactory, builder, true);
	JsonBuilderUtils.add(builderFactory, builder, "string");
	JsonBuilderUtils.add(builderFactory, builder, 'c');
	JsonBuilderUtils.add(builderFactory, builder, BigInteger.TEN);
	JsonBuilderUtils.add(builderFactory, builder,
			BigDecimal.valueOf(1111, 2));
	JsonBuilderUtils.add(builderFactory, builder, 22f);
	JsonBuilderUtils.add(builderFactory, builder, 22d);
	JsonBuilderUtils.add(builderFactory, builder, (byte) 33);
	JsonBuilderUtils.add(builderFactory, builder, (int) 44);
	JsonBuilderUtils.add(builderFactory, builder, (int) 55);
	JsonBuilderUtils.add(builderFactory, builder,
			Arrays.<Object> asList("a", 0xbc, "d"));
	JsonBuilderUtils.add(builderFactory, builder, new Object[] { 1, "2", 3,
			false });

	JsonBuilderUtils.add(builderFactory, builder,
			Collections.singletonMap("foo", "bar"));
	// provider.createWriter(System.out).write(builder.build());
}
 
Example 8
Source File: JsonpWriter.java    From incubator-batchee with Apache License 2.0 5 votes vote down vote up
@Override
public void open(final Serializable checkpoint) throws Exception {
    final ClassLoader loader = Thread.currentThread().getContextClassLoader();
    final JsonProvider provider = this.provider == null ? JsonProvider.provider() : JsonProvider.class.cast(loader.loadClass(this.provider));

    final File outputFile = new File(file);
    if (!outputFile.getParentFile().exists() && !outputFile.getParentFile().mkdirs()) {
        throw new BatchRuntimeException("Can't create " + outputFile.getAbsolutePath());
    }

    writer = new TransactionalWriter(outputFile, encoding, checkpoint);
    generator = provider.createGeneratorFactory(buildConfig()).createGenerator(writer);
    if (fieldNameGeneratorClass != null) {
        if ("default".equals(fieldNameGeneratorClass)) {
            fieldNameGenerator = new FieldNameGenerator() {
                private int count = 0;

                @Override
                public String nextName() {
                    return "item" + ++count;
                }
            };
        } else {
            fieldNameGenerator = FieldNameGenerator.class.cast(Thread.currentThread().getContextClassLoader().loadClass(fieldNameGeneratorClass).newInstance());
        }
    }

    if (useGlobalWrapper()) {
        if (fieldNameGenerator != null) {
            generator.writeStartObject();
        } else {
            generator.writeStartArray();
        }
    }
}
 
Example 9
Source File: JsonPartialReader.java    From incubator-batchee with Apache License 2.0 4 votes vote down vote up
public JsonPartialReader(final JsonProvider provider, final JsonParser parser) {
    this.parser = parser;
    this.provider = provider == null ? JsonProvider.provider() : provider;
}