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

The following examples show how to use com.dslplatform.json.runtime.Settings#withRuntime() . 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: DateTest.java    From dsl-json with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void nineDigitsInAODTClass() throws IOException {
	NineODT n = new NineODT();
	n.at = OffsetDateTime.parse("1930-09-04T00:03:48.750431006Z");
	DslJson<Object> dslJson = new DslJson<>(Settings.withRuntime());
	ByteArrayOutputStream os = new ByteArrayOutputStream();
	dslJson.serialize(n, os);;
	Assert.assertEquals("{\"at\":\"1930-09-04T00:03:48.750431006Z\"}", os.toString());
}
 
Example 2
Source File: DateTest.java    From dsl-json with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void nineDigitsInAOTClass() throws IOException {
	NineOT n = new NineOT();
	n.at = OffsetTime.parse("01:33:08.750431006Z");
	DslJson<Object> dslJson = new DslJson<>(Settings.withRuntime());
	ByteArrayOutputStream os = new ByteArrayOutputStream();
	dslJson.serialize(n, os);;
	Assert.assertEquals("{\"at\":\"01:33:08.750431006Z\"}", os.toString());
}
 
Example 3
Source File: DateTest.java    From dsl-json with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void sqlDateWillNotExplode() throws IOException {
	java.util.Date ud = new java.util.Date(119, 2, 10);
	SqlDate sql = new SqlDate();
	sql.date = new java.sql.Date(ud.getTime());
	DslJson<Object> dslJson = new DslJson<>(Settings.withRuntime());
	ByteArrayOutputStream os = new ByteArrayOutputStream();
	dslJson.serialize(sql, os);
	Assert.assertEquals("{\"date\":\"2019-03-10\"}", os.toString());
}
 
Example 4
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 {

		//since annotation processor is disabled in pom.xml only reflection will be used.
		//even if annotation processor was not disabled, it would still run in reflection mode unless
		//.includeServiceLoader() is called on Settings
		DslJson<Object> dslJson = new DslJson<>(Settings.withRuntime()); //Runtime configuration needs to be explicitly enabled
		//writer should be reused. For per thread reuse use ThreadLocal pattern
		JsonWriter writer = dslJson.newWriter();

		Model instance = new Model();
		instance.string = "Hello World!";
		instance.number = 42;
		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.nested = Arrays.asList(new Model.Nested(), null);
		instance.inheritance = new Model.ParentClass();
		instance.inheritance.a = 5;
		instance.inheritance.b = 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.map = new HashMap<>();
		instance.map.put("abc", 678);
		instance.map.put("array", new int[] { 2, 4, 8});

		dslJson.serialize(writer, instance);

		//resulting buffer with JSON
		byte[] buffer = writer.getByteBuffer();
		//end of buffer
		int size = writer.size();
		System.out.println(writer);

		//deserialization using byte[] API
		Model deser = dslJson.deserialize(Model.class, buffer, size);

		System.out.println(deser.string);
	}