use of com.fasterxml.jackson.core.JsonGenerator in project ig-json-parser by Instagram.
the class SerializeTest method serializeWithGetterTest.
@Test
public void serializeWithGetterTest() throws IOException {
GetterUUT source = new GetterUUT();
source.intField = 5;
StringWriter stringWriter = new StringWriter();
JsonGenerator jsonGenerator = new JsonFactory().createGenerator(stringWriter);
GetterUUT__JsonHelper.serializeToJson(jsonGenerator, source, true);
jsonGenerator.close();
String inputString = stringWriter.toString();
GetterUUT parsed = GetterUUT__JsonHelper.parseFromJson(inputString);
assertEquals(10, parsed.intField);
}
use of com.fasterxml.jackson.core.JsonGenerator in project ig-json-parser by Instagram.
the class SerializeTest method simpleSerializeTest.
@Test
public void simpleSerializeTest() throws IOException, JSONException {
final int intValue = 25;
final int integerValue = 37;
final String stringValue = "hello world\r\n\'\"";
final List<Integer> integerList = 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);
SimpleParseUUT source = new SimpleParseUUT();
source.intField = intValue;
source.integerField = integerValue;
source.stringField = stringValue;
source.integerListField = integerList;
source.integerQueueField = integerQueue;
source.integerSetField = integerSet;
source.subobjectField = new SimpleParseUUT.SubobjectParseUUT();
source.subobjectField.intField = subIntValue;
source.subenumField = subEnum;
source.subenumFieldList = subEnumList;
StringWriter stringWriter = new StringWriter();
JsonGenerator jsonGenerator = new JsonFactory().createGenerator(stringWriter);
SimpleParseUUT__JsonHelper.serializeToJson(jsonGenerator, source, true);
jsonGenerator.close();
String inputString = stringWriter.toString();
JsonParser jp = new JsonFactory().createParser(inputString);
jp.nextToken();
SimpleParseUUT parsed = SimpleParseUUT__JsonHelper.parseFromJson(jp);
assertSame(source.intField, parsed.intField);
assertEquals(source.integerField, parsed.integerField);
assertEquals(source.stringField, parsed.stringField);
assertEquals(source.integerListField, parsed.integerListField);
// NOTE: this is because ArrayDeque hilariously does not implement .equals()/.hashcode().
assertEquals(Lists.newArrayList(source.integerQueueField), Lists.newArrayList(parsed.integerQueueField));
assertEquals(source.integerSetField, parsed.integerSetField);
assertSame(source.subobjectField.intField, parsed.subobjectField.intField);
assertSame(source.subenumField, parsed.subenumField);
assertEquals(source.subenumFieldList, parsed.subenumFieldList);
}
use of com.fasterxml.jackson.core.JsonGenerator in project jackson-databind by FasterXML.
the class SerializationFeaturesTest method testFlushingNotAutomatic.
public void testFlushingNotAutomatic() throws IOException {
// but should not occur if configured otherwise
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.FLUSH_AFTER_WRITE_VALUE, false);
StringWriter sw = new StringWriter();
JsonGenerator g = mapper.getFactory().createGenerator(sw);
mapper.writeValue(g, Integer.valueOf(13));
// no flushing now:
assertEquals("", sw.toString());
// except when actually flushing
g.flush();
assertEquals("13", sw.toString());
g.close();
// Also, same should happen with ObjectWriter
sw = new StringWriter();
g = mapper.getFactory().createGenerator(sw);
ObjectWriter ow = mapper.writer();
ow.writeValue(g, Integer.valueOf(99));
assertEquals("", sw.toString());
// except when actually flushing
g.flush();
assertEquals("99", sw.toString());
g.close();
}
use of com.fasterxml.jackson.core.JsonGenerator in project opentsdb by OpenTSDB.
the class Annotation method getStorageJSON.
/**
* Serializes the object in a uniform matter for storage. Needed for
* successful CAS calls
* @return The serialized object as a byte array
*/
@VisibleForTesting
byte[] getStorageJSON() {
// TODO - precalculate size
final ByteArrayOutputStream output = new ByteArrayOutputStream();
try {
final JsonGenerator json = JSON.getFactory().createGenerator(output);
json.writeStartObject();
if (tsuid != null && !tsuid.isEmpty()) {
json.writeStringField("tsuid", tsuid);
}
json.writeNumberField("startTime", start_time);
json.writeNumberField("endTime", end_time);
json.writeStringField("description", description);
json.writeStringField("notes", notes);
if (custom == null) {
json.writeNullField("custom");
} else {
final TreeMap<String, String> sorted_custom = new TreeMap<String, String>(custom);
json.writeObjectField("custom", sorted_custom);
}
json.writeEndObject();
json.close();
return output.toByteArray();
} catch (IOException e) {
throw new RuntimeException("Unable to serialize Annotation", e);
}
}
use of com.fasterxml.jackson.core.JsonGenerator in project opentsdb by OpenTSDB.
the class TSMeta method getStorageJSON.
/**
* Formats the JSON output for writing to storage. It drops objects we don't
* need or want to store (such as the UIDMeta objects or the total dps) to
* save space. It also serializes in order so that we can make a proper CAS
* call. Otherwise the POJO serializer may place the fields in any order
* and CAS calls would fail all the time.
* @return A byte array to write to storage
*/
private byte[] getStorageJSON() {
// 256 bytes is a good starting value, assumes default info
final ByteArrayOutputStream output = new ByteArrayOutputStream(256);
try {
final JsonGenerator json = JSON.getFactory().createGenerator(output);
json.writeStartObject();
json.writeStringField("tsuid", tsuid);
json.writeStringField("displayName", display_name);
json.writeStringField("description", description);
json.writeStringField("notes", notes);
json.writeNumberField("created", created);
if (custom == null) {
json.writeNullField("custom");
} else {
json.writeObjectFieldStart("custom");
for (Map.Entry<String, String> entry : custom.entrySet()) {
json.writeStringField(entry.getKey(), entry.getValue());
}
json.writeEndObject();
}
json.writeStringField("units", units);
json.writeStringField("dataType", data_type);
json.writeNumberField("retention", retention);
json.writeNumberField("max", max);
json.writeNumberField("min", min);
json.writeEndObject();
json.close();
return output.toByteArray();
} catch (IOException e) {
throw new RuntimeException("Unable to serialize TSMeta", e);
}
}
Aggregations