Java Code Examples for com.dslplatform.json.runtime.Settings#basicSetup()

The following examples show how to use com.dslplatform.json.runtime.Settings#basicSetup() . 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: OptionalTest.java    From dsl-json with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void lazyOptionalResolution() throws IOException {
	DslJson<Object> lazy = new DslJson<Object>(Settings.basicSetup());
	JsonReader.ReadObject decoder = lazy.tryFindReader(new TypeDefinition<Optional<LocalDate>>() {}.type);
	JsonWriter.WriteObject encoder = lazy.tryFindWriter(new TypeDefinition<Optional<LocalDate>>() {}.type);
	Assert.assertNotNull(decoder);
	Assert.assertNotNull(encoder);
	JsonWriter writer = lazy.newWriter();
	LocalDate date = LocalDate.of(2018, 7, 26);
	encoder.write(writer, Optional.of(date));
	Assert.assertEquals("\"2018-07-26\"", writer.toString());
	JsonReader<Object> reader = lazy.newReader(writer.toByteArray());
	reader.read();
	Optional<LocalDate> result = (Optional<LocalDate>) decoder.read(reader);
	Assert.assertEquals(date, result.get());
}
 
Example 2
Source File: Example.java    From dsl-json with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public static void main(String[] args) throws IOException {

		//Model converters will be loaded based on naming convention.
		//Previously it would be loaded through ServiceLoader.load,
		//which is still an option if dsljson.configuration name is specified.
		//DSL-JSON loads all services registered into META-INF/services
		//and falls back to naming based convention of package._NAME_DslJsonConfiguration if not found
		//Annotation processor will run by default and generate descriptions for JSON encoding/decoding
		//To include Jackson annotations dsljson.jackson=true must be passed to annotation processor
		//When conversion is not fully supported by compiler Settings.basicSetup() can be enabled to support runtime analysis
		//for features not registered by annotation processor. Currently it is enabled due to use of Set and Vector
		DslJson<Object> dslJson = new DslJson<>(Settings.basicSetup());

		Model instance = new Model();
		instance.string = "Hello World!";
		instance.integers = Arrays.asList(1, 2, 3);
		instance.decimals = new HashSet<>(Arrays.asList(BigDecimal.ONE, BigDecimal.ZERO));
		instance.uuids = new UUID[]{new UUID(1L, 2L), new UUID(3L, 4L)};
		instance.longs = new Vector<>(Arrays.asList(1L, 2L));
		instance.inheritance = new Model.ParentClass();
		instance.inheritance.a = 5;
		instance.inheritance.b = 6;
		instance.iface = new Model.WithCustomCtor(5, 6);
		instance.person = new ImmutablePerson("first name", "last name", 35);
		instance.states = Arrays.asList(Model.State.HI, Model.State.LOW);
		instance.jsonObject = new Model.JsonObjectReference(43, "abcd");
		instance.jsonObjects = Collections.singletonList(new Model.JsonObjectReference(34, "dcba"));
		instance.intList = new ArrayList<>(Arrays.asList(123, 456));
		instance.map = new HashMap<>();
		instance.map.put("abc", 678);
		instance.map.put("array", new int[] { 2, 4, 8});
		instance.factories = Arrays.asList(null, Model.ViaFactory.create("me", 2), Model.ViaFactory.create("you", 3), null);
		instance.builder = PersonBuilder.builder().firstName("first").lastName("last").age(42).build();

		ByteArrayOutputStream os = new ByteArrayOutputStream();
		dslJson.serialize(instance, os);
		System.out.println(os);

		ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
		//deserialization using Stream API
		Model deser = dslJson.deserialize(Model.class, is);

		System.out.println(deser.string);
	}
 
Example 3
Source File: GenericTest.java    From dsl-json with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
@Test
public void willUseAnnotationProcessorVersion() throws IOException {

	DslJson<Object> customJson = new DslJson<>(Settings.basicSetup());

	byte[] bytes = "{\"VALUE\":\"ABC\"}".getBytes("UTF-8");

	Type type = new TypeDefinition<GenericFromAnnotation<String>>() {}.type;
	GenericFromAnnotation<String> result = (GenericFromAnnotation<String>)customJson.deserialize(type, bytes, bytes.length);

	Assert.assertEquals("ABC", result.value);
}