use of com.instagram.common.json.annotation.processor.support.ExtensibleJSONWriter in project ig-json-parser by Instagram.
the class DeserializeTest method simpleDeserializeTest.
@Test
public void simpleDeserializeTest() throws IOException, JSONException {
final int intValue = 25;
final int integerValue = 37;
final float floatValue = 1.0f;
final float floatObjValue = 2.0f;
final String stringValue = "hello world\r\n\'\"";
final List<Integer> integerList = Lists.newArrayList(1, 2, 3, 4);
final ArrayList<Integer> integerArrayList = Lists.newArrayList(1, 2, 3, 4);
final Queue<Integer> integerQueue = Queues.newArrayDeque(Arrays.asList(1, 2, 3, 4));
final Set<Integer> integerSet = Sets.newHashSet(1, 2, 3, 4);
final int subIntValue = 30;
final SimpleParseUUT.SubenumUUT subEnum = SimpleParseUUT.SubenumUUT.A;
final List<SimpleParseUUT.SubenumUUT> subEnumList = Lists.newArrayList(SimpleParseUUT.SubenumUUT.A, SimpleParseUUT.SubenumUUT.B);
StringWriter stringWriter = new StringWriter();
ExtensibleJSONWriter writer = new ExtensibleJSONWriter(stringWriter);
writer.object().key(SimpleParseUUT.INT_FIELD_NAME).value(intValue).key(SimpleParseUUT.INTEGER_FIELD_NAME).value(integerValue).key(SimpleParseUUT.FLOAT_FIELD_NAME).value(floatValue).key(SimpleParseUUT.FLOAT_OBJ_FIELD_NAME).value(floatObjValue).key(SimpleParseUUT.STRING_FIELD_NAME).value(stringValue).key(SimpleParseUUT.INTEGER_LIST_FIELD_NAME).array().extend(new ExtensibleJSONWriter.Extender() {
@Override
public void extend(ExtensibleJSONWriter writer) throws JSONException {
for (Integer integer : integerList) {
writer.value(integer);
}
}
}).endArray().key(SimpleParseUUT.INTEGER_ARRAY_LIST_FIELD_NAME).array().extend(new ExtensibleJSONWriter.Extender() {
@Override
public void extend(ExtensibleJSONWriter writer) throws JSONException {
for (Integer integer : integerList) {
writer.value(integer);
}
}
}).endArray().key(SimpleParseUUT.INTEGER_QUEUE_FIELD_NAME).array().extend(new ExtensibleJSONWriter.Extender() {
@Override
public void extend(ExtensibleJSONWriter writer) throws JSONException {
for (Integer integer : integerQueue) {
writer.value(integer);
}
}
}).endArray().key(SimpleParseUUT.INTEGER_SET_FIELD_NAME).array().extend(new ExtensibleJSONWriter.Extender() {
@Override
public void extend(ExtensibleJSONWriter writer) throws JSONException {
for (Integer integer : integerSet) {
writer.value(integer);
}
}
}).endArray().key(SimpleParseUUT.SUBOBJECT_FIELD_NAME).object().key(SimpleParseUUT.SubobjectParseUUT.INT_FIELD_NAME).value(subIntValue).endObject().key(SimpleParseUUT.SUBENUM_FIELD_NAME).value(subEnum.toString()).key(SimpleParseUUT.SUBENUM_LIST_FIELD_NAME).array().extend(new ExtensibleJSONWriter.Extender() {
@Override
public void extend(ExtensibleJSONWriter writer) throws JSONException {
for (SimpleParseUUT.SubenumUUT enumValue : subEnumList) {
writer.value(enumValue.toString());
}
}
}).endArray().endObject();
String inputString = stringWriter.toString();
JsonParser jp = new JsonFactory().createParser(inputString);
jp.nextToken();
SimpleParseUUT uut = SimpleParseUUT__JsonHelper.parseFromJson(jp);
assertTrue(new SimpleParseUUT__JsonHelper() instanceof JsonHelper);
assertSame(intValue, uut.intField);
assertSame(integerValue, uut.integerField.intValue());
assertEquals(Float.valueOf(floatValue), Float.valueOf(uut.floatField));
assertEquals(Float.valueOf(floatObjValue), uut.FloatField);
assertEquals(stringValue, uut.stringField);
assertEquals(integerList, uut.integerListField);
assertEquals(integerArrayList, uut.integerArrayListField);
// NOTE: this is because ArrayDeque hilariously does not implement .equals()/.hashcode().
assertEquals(Lists.newArrayList(integerQueue), Lists.newArrayList(uut.integerQueueField));
assertEquals(integerSet, uut.integerSetField);
assertSame(subIntValue, uut.subobjectField.intField);
assertSame(subEnum, uut.subenumField);
assertEquals(subEnumList, uut.subenumFieldList);
}
use of com.instagram.common.json.annotation.processor.support.ExtensibleJSONWriter in project ig-json-parser by Instagram.
the class DeserializeTest method parseObjectFromContents.
/**
* Write an object in which the contents are sourced from the extender. Then parse it as an
* {@link ExactMappingUUT} object and return it.
*/
private static ExactMappingUUT parseObjectFromContents(ExtensibleJSONWriter.Extender extender) throws IOException, JSONException {
StringWriter stringWriter = new StringWriter();
ExtensibleJSONWriter writer = new ExtensibleJSONWriter(stringWriter);
writer.object().extend(extender).endObject();
return ExactMappingUUT__JsonHelper.parseFromJson(stringWriter.toString());
}
use of com.instagram.common.json.annotation.processor.support.ExtensibleJSONWriter in project ig-json-parser by Instagram.
the class DeserializeTest method malformedArrayEntry.
@Test
public void malformedArrayEntry() throws IOException, JSONException {
final List<Integer> integerList = Lists.newArrayList(1, null, 3, 4);
final int subIntValue = 30;
StringWriter stringWriter = new StringWriter();
ExtensibleJSONWriter writer = new ExtensibleJSONWriter(stringWriter);
writer.object().key(StrictListParseUUT.INTEGER_LIST_FIELD_NAME).array().extend(new ExtensibleJSONWriter.Extender() {
@Override
public void extend(ExtensibleJSONWriter writer) throws JSONException {
for (Integer integer : integerList) {
writer.value(integer);
}
}
}).endArray().key(StrictListParseUUT.SUBOBJECT_LIST_FIELD_NAME).object().key(StrictListParseUUT.SubobjectParseUUT.INT_FIELD_NAME).value(subIntValue).endObject().endObject();
String inputString = stringWriter.toString();
JsonParser jp = new JsonFactory().createParser(inputString);
jp.nextToken();
StrictListParseUUT uut = StrictListParseUUT__JsonHelper.parseFromJson(jp);
assertEquals(3, uut.integerListField.size());
assertEquals(1, uut.integerListField.get(0).intValue());
assertEquals(3, uut.integerListField.get(1).intValue());
assertEquals(4, uut.integerListField.get(2).intValue());
}
use of com.instagram.common.json.annotation.processor.support.ExtensibleJSONWriter in project ig-json-parser by Instagram.
the class MalformedJsonTest method scalarInsteadOfArray.
@Test
public void scalarInsteadOfArray() throws IOException, JSONException {
final int intValue = 25;
final int integerValue = 37;
final String stringValue = "hello world\r\n\'\"";
final int subIntValue = 30;
StringWriter stringWriter = new StringWriter();
ExtensibleJSONWriter writer = new ExtensibleJSONWriter(stringWriter);
writer.object().key(SimpleParseUUT.INT_FIELD_NAME).value(intValue).key(SimpleParseUUT.INTEGER_FIELD_NAME).value(integerValue).key(SimpleParseUUT.STRING_FIELD_NAME).value(stringValue).key(SimpleParseUUT.INTEGER_LIST_FIELD_NAME).value(intValue).key(SimpleParseUUT.SUBOBJECT_FIELD_NAME).object().key(SimpleParseUUT.SubobjectParseUUT.INT_FIELD_NAME).value(subIntValue).endObject().endObject();
String inputString = stringWriter.toString();
JsonParser jp = new JsonFactory().createParser(inputString);
jp.nextToken();
SimpleParseUUT uut = SimpleParseUUT__JsonHelper.parseFromJson(jp);
assertSame(intValue, uut.intField);
assertSame(integerValue, uut.integerField.intValue());
assertEquals(stringValue, uut.stringField);
assertNull(uut.integerListField);
assertSame(subIntValue, uut.subobjectField.intField);
}
use of com.instagram.common.json.annotation.processor.support.ExtensibleJSONWriter in project ig-json-parser by Instagram.
the class DeserializeTest method testCustomParse.
@Test
public void testCustomParse() throws Exception {
String value = "hey there";
StringWriter stringWriter = new StringWriter();
ExtensibleJSONWriter writer = new ExtensibleJSONWriter(stringWriter);
writer.object().key(CustomParseContainerUUT.INNER_FIELD_NAME).object().key(CustomParseContainerUUT.CustomParseUUT.STRING_FIELD_NAME).value(value).endObject().endObject();
String inputString = stringWriter.toString();
JsonParser jp = new JsonFactory().createParser(inputString);
jp.nextToken();
CustomParseContainerUUT uut = CustomParseContainerUUT__JsonHelper.parseFromJson(jp);
assertEquals(value, uut.getCustomParseUUT().getStringField());
}
Aggregations