com.dslplatform.json.DslJson Java Examples

The following examples show how to use com.dslplatform.json.DslJson. 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: OptionalAnalyzer.java    From dsl-json with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Nullable
private static OptionalEncoder analyzeEncoding(final Type manifest, final Type content, final Class<?> raw, final DslJson json) {
	if (raw != Optional.class) {
		return null;
	} else if (content == Optional.class) {
		final OptionalEncoder nested = analyzeEncoding(content, Object.class, Optional.class, json);
		json.registerWriter(manifest, nested);
		return nested;
	}
	final JsonWriter.WriteObject<?> writer = Object.class == content ? null : json.tryFindWriter(content);
	if (Object.class != content && writer == null) {
		return null;
	}
	final OptionalEncoder encoder = new OptionalEncoder<>(json, Object.class == content ? null : writer);
	json.registerWriter(manifest, encoder);
	return encoder;
}
 
Example #2
Source File: OptionalAnalyzer.java    From dsl-json with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Nullable
private static OptionalDecoder analyzeDecoding(final Type manifest, final Type content, final Class<?> raw, final DslJson json) {
	if (raw != Optional.class) {
		return null;
	} else if (content == Optional.class) {
		final OptionalDecoder nested = analyzeDecoding(content, Object.class, Optional.class, json);
		final OptionalDecoder outer = new OptionalDecoder<>(nested);
		json.registerReader(manifest, outer);
		return outer;
	}
	final JsonReader.ReadObject<?> reader = json.tryFindReader(content);
	if (reader == null) {
		return null;
	}
	final OptionalDecoder decoder = new OptionalDecoder<>(reader);
	json.registerReader(manifest, decoder);
	return decoder;
}
 
Example #3
Source File: HexUtilsTest.java    From apm-agent-java with Apache License 2.0 5 votes vote down vote up
@Test
void testLongToHex() {
    byte[] bytes = new byte[8];
    HexUtils.nextBytes("09c2572177fdae24", 0, bytes);
    long l = ByteUtils.getLong(bytes, 0);
    JsonWriter jw = new DslJson<>().newWriter();
    HexUtils.writeAsHex(l, jw);
    assertThat(jw.toString()).isEqualTo("09c2572177fdae24");
}
 
Example #4
Source File: DSL.java    From dsl-json with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static DslJson<Object> JSON() {
    if (json == null) {
        //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
        //basicSetup is Android friendly version of withRuntime (which avoids Java8 types)
        //It is enabled to support runtime analysis for stuff which is not registered by default
        //Annotation processor will run by default and generate descriptions for JSON encoding/decoding
        json = new DslJson<>(Settings.basicSetup().allowArrayFormat(true));
    }
    return json;
}
 
Example #5
Source File: DSL.java    From dsl-json with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static DslJson<Object> JSON() {
    if (json == null) {
        //during initialization ServiceLoader.load should pick up services registered into META-INF/services
        //this doesn't really work on Android so DslJson will fallback to default generated class name
        //"dsl_json.json.ExternalSerialization" and try to initialize it manually
        json = new DslJson<>();
    }
    return json;
}
 
Example #6
Source File: OptionalAnalyzer.java    From dsl-json with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Nullable
@Override
public OptionalEncoder tryCreate(Type manifest, DslJson dslJson) {
	if (manifest instanceof ParameterizedType) {
		final ParameterizedType pt = (ParameterizedType) manifest;
		if (pt.getActualTypeArguments().length == 1) {
			return analyzeEncoding(manifest, pt.getActualTypeArguments()[0], (Class<?>) pt.getRawType(), dslJson);
		}
	}
	if (manifest == Optional.class) {
		return analyzeEncoding(manifest, Object.class, Optional.class, dslJson);
	}
	return null;
}
 
Example #7
Source File: OptionalAnalyzer.java    From dsl-json with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Nullable
@Override
public OptionalDecoder tryCreate(Type manifest, DslJson dslJson) {
	if (manifest instanceof ParameterizedType) {
		final ParameterizedType pt = (ParameterizedType) manifest;
		if (pt.getActualTypeArguments().length == 1) {
			return analyzeDecoding(manifest, pt.getActualTypeArguments()[0], (Class<?>) pt.getRawType(), dslJson);
		}
	}
	if (manifest == Optional.class) {
		return analyzeDecoding(manifest, Object.class, Optional.class, dslJson);
	}
	return null;
}
 
Example #8
Source File: LazyAttributeDecoder.java    From dsl-json with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
LazyAttributeDecoder(
		final Settings.BiConsumer<T, P> write,
		final DslJson json,
		final Type type) {
	if (write == null) throw new IllegalArgumentException("write can't be null");
	if (json == null) throw new IllegalArgumentException("json can't be null");
	if (type == null) throw new IllegalArgumentException("type can't be null");
	this.write = write;
	this.json = json;
	this.type = type;
}
 
Example #9
Source File: EnumAnalyzer.java    From dsl-json with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Nullable
private static EnumDescription analyze(final Type manifest, final Class<Enum> raw, final DslJson json) {
	if (raw.isArray()
			|| Collection.class.isAssignableFrom(raw)
			|| (raw.getModifiers() & Modifier.ABSTRACT) != 0
			|| (raw.getDeclaringClass() != null && (raw.getModifiers() & Modifier.STATIC) == 0)) {
		return null;
	}
	final EnumDescription converter = new EnumDescription<>(raw, raw.getEnumConstants());
	json.registerWriter(manifest, converter);
	json.registerReader(manifest, converter);
	return converter;
}
 
Example #10
Source File: EnumAnalyzer.java    From dsl-json with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Nullable
@Override
public EnumDescription tryCreate(Type manifest, DslJson dslJson) {
	if (manifest instanceof Class<?> && ((Class<?>) manifest).isEnum()) {
		return analyze(manifest, (Class<Enum>) manifest, dslJson);
	}
	if (manifest instanceof ParameterizedType) {
		final ParameterizedType pt = (ParameterizedType) manifest;
		if (pt.getActualTypeArguments().length == 1
				&& ((Class<?>) pt.getRawType()).isEnum()) {
			return analyze(manifest, (Class<Enum>) pt.getRawType(), dslJson);
		}
	}
	return null;
}
 
Example #11
Source File: AbstractJsonSerializer.java    From gridgo with MIT License 5 votes vote down vote up
protected AbstractJsonSerializer(@NonNull JsonCompactMode compactMode) {

        var skipNull = compactMode == JsonCompactMode.COMPACT;
        var settings = new Settings<Object>().includeServiceLoader().skipDefaultValues(skipNull);

        dslJson = new DslJson<>(settings);
        dslJson.registerWriter(BElement.class, skipNull ? COMPACT::write : NO_COMPACT::write);
        dslJson.registerReader(BElement.class, skipNull ? COMPACT::read : NO_COMPACT::read);
    }
 
Example #12
Source File: ZonedDateTimeDslJsonConverter.java    From dsl-json with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void configure(DslJson json) {
	json.registerReader(ZonedDateTime.class, ThreetenbpConverter.ZONED_DATE_TIME_READER);
	json.registerWriter(ZonedDateTime.class, ThreetenbpConverter.ZONED_DATE_TIME_WRITER);
}
 
Example #13
Source File: BitmapDslJsonConverter.java    From dsl-json with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void configure(DslJson json) {
	json.registerReader(android.graphics.Bitmap.class, AndroidGeomConverter.IMAGE_READER);
	json.registerWriter(android.graphics.Bitmap.class, AndroidGeomConverter.IMAGE_WRITER);
}
 
Example #14
Source File: PointDslJsonConverter.java    From dsl-json with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void configure(DslJson json) {
	json.registerReader(android.graphics.Point.class, AndroidGeomConverter.POINT_READER);
	json.registerWriter(android.graphics.Point.class, AndroidGeomConverter.POINT_WRITER);
}
 
Example #15
Source File: PointFDslJsonConverter.java    From dsl-json with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void configure(DslJson json) {
	json.registerReader(android.graphics.PointF.class, AndroidGeomConverter.LOCATION_READER);
	json.registerWriter(android.graphics.PointF.class, AndroidGeomConverter.LOCATION_WRITER);
}
 
Example #16
Source File: RectDslJsonConverter.java    From dsl-json with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void configure(DslJson json) {
	json.registerReader(android.graphics.Rect.class, AndroidGeomConverter.RECTANGLE_READER);
	json.registerWriter(android.graphics.Rect.class, AndroidGeomConverter.RECTANGLE_WRITER);
}
 
Example #17
Source File: DslJsonSerializer.java    From apm-agent-java with Apache License 2.0 4 votes vote down vote up
public DslJsonSerializer(StacktraceConfiguration stacktraceConfiguration, ApmServerClient apmServerClient) {
    this.stacktraceConfiguration = stacktraceConfiguration;
    this.apmServerClient = apmServerClient;
    jw = new DslJson<>(new DslJson.Settings<>()).newWriter(BUFFER_SIZE);
}
 
Example #18
Source File: MainActivity.java    From dsl-json with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    DslJson<Object> dslJson = DSL.JSON();

    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.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.time = LocalTime.of(12, 15);
    instance.times = Arrays.asList(null, LocalTime.of(8, 16));
    Model.Concrete concrete = new Model.Concrete();
    concrete.x = 11;
    concrete.y = 23;
    instance.abs = concrete;
    instance.absList = Arrays.<Model.Abstract>asList(concrete, null, concrete);
    instance.decimal2 = BigDecimal.TEN;
    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();
    instance.numbers = Arrays.asList(SpecialNumber.E, SpecialNumber.PI, SpecialNumber.ZERO);

    TextView tv = (TextView)findViewById(R.id.tvHello);
    try {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        //serialize into stream
        dslJson.serialize(instance, os);

        ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
        //deserialized using stream API
        Model deserialized = dslJson.deserialize(Model.class, is);

        tv.setText(deserialized.string);
    } catch (IOException ex) {
        tv.setText(ex.getMessage());
    }
}
 
Example #19
Source File: ClientsJsonProvider.java    From java-json-benchmark with MIT License 4 votes vote down vote up
@Override
public DslJson<Object> dsljson() {
    return dsljson;
}
 
Example #20
Source File: MainActivity.java    From dsl-json with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    DslJson<Object> dslJson = DSL.JSON();

    Model instance = new Model();
    instance.number = 42;
    instance.string = "Hello World!";
    instance.integers = Arrays.asList(1, 2, 3);
    instance.decimals = new HashSet<BigDecimal>(Arrays.asList(BigDecimal.ONE, BigDecimal.ZERO));
    instance.uuids = new UUID[]{new UUID(1L, 2L), new UUID(3L, 4L)};
    instance.longs = new Vector<Long>(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.date = new Date();
    instance.dates = Arrays.asList(null, new Date(0));
    Model.Concrete concrete = new Model.Concrete();
    concrete.x = 11;
    concrete.y = 23;
    instance.abs = concrete;
    instance.absList = Arrays.<Model.Abstract>asList(concrete, null, concrete);
    instance.decimal2 = BigDecimal.TEN;
    instance.intList = new ArrayList<Integer>(Arrays.asList(123, 456));
    instance.map = new HashMap<String, Object>();
    instance.map.put("abc", 678);
    instance.map.put("array", new int[] { 2, 4, 8});
    TextView tv = (TextView)findViewById(R.id.tvHello);
    try {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        //serialize into stream
        dslJson.serialize(instance, os);

        ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
        //deserialized using stream API
        Model deserialized = dslJson.deserialize(Model.class, is);

        tv.setText(deserialized.string);
    } catch (IOException ex) {
        tv.setText(ex.getMessage());
    }
}
 
Example #21
Source File: LocalDateDslJsonConverter.java    From dsl-json with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void configure(DslJson json) {
	json.registerReader(org.joda.time.LocalDate.class, JodaTimeConverter.LOCAL_DATE_READER);
	json.registerWriter(org.joda.time.LocalDate.class, JodaTimeConverter.LOCAL_DATE_WRITER);
}
 
Example #22
Source File: DateTimeDslJsonConverter.java    From dsl-json with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void configure(DslJson json) {
	json.registerReader(org.joda.time.DateTime.class, JodaTimeConverter.DATE_TIME_READER);
	json.registerWriter(org.joda.time.DateTime.class, JodaTimeConverter.DATE_TIME_WRITER);
}
 
Example #23
Source File: LocalDateDslJsonConverter.java    From dsl-json with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void configure(DslJson json) {
	json.registerReader(LocalDate.class, ThreetenbpConverter.LOCAL_DATE_READER);
	json.registerWriter(LocalDate.class, ThreetenbpConverter.LOCAL_DATE_WRITER);
}
 
Example #24
Source File: OffsetDateTimeDslJsonConverter.java    From dsl-json with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void configure(DslJson json) {
	json.registerReader(OffsetDateTime.class, ThreetenbpConverter.DATE_TIME_READER);
	json.registerWriter(OffsetDateTime.class, ThreetenbpConverter.DATE_TIME_WRITER);
}
 
Example #25
Source File: LocalTimeDslJsonConverter.java    From dsl-json with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void configure(DslJson json) {
	json.registerReader(LocalTime.class, JavaTimeConverter.LOCAL_TIME_READER);
	json.registerWriter(LocalTime.class, JavaTimeConverter.LOCAL_TIME_WRITER);
}
 
Example #26
Source File: LocalDateTimeDslJsonConverter.java    From dsl-json with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void configure(DslJson json) {
	json.registerReader(LocalDateTime.class, ThreetenbpConverter.LOCAL_DATE_TIME_READER);
	json.registerWriter(LocalDateTime.class, ThreetenbpConverter.LOCAL_DATE_TIME_WRITER);
}
 
Example #27
Source File: ClientsJsonProvider.java    From java-json-benchmark with MIT License 4 votes vote down vote up
@Override
public DslJson<Object> dsljson_reflection() {
    return dsljson_reflection;
}
 
Example #28
Source File: UsersJsonProvider.java    From java-json-benchmark with MIT License 4 votes vote down vote up
@Override
public DslJson<Object> dsljson() {
    return dsljson;
}
 
Example #29
Source File: UsersJsonProvider.java    From java-json-benchmark with MIT License 4 votes vote down vote up
@Override
public DslJson<Object> dsljson_reflection() {
    return dsljson_reflection;
}
 
Example #30
Source File: LocalDateTimeDslJsonConverter.java    From dsl-json with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void configure(DslJson json) {
	json.registerReader(LocalDateTime.class, JavaTimeConverter.LOCAL_DATE_TIME_READER);
	json.registerWriter(LocalDateTime.class, JavaTimeConverter.LOCAL_DATE_TIME_WRITER);
}