80 lines
2.2 KiB
Java
80 lines
2.2 KiB
Java
|
package eu.dnetlib.ariadneplus.registry;
|
||
|
|
||
|
import java.io.IOException;
|
||
|
import java.io.StringWriter;
|
||
|
import java.util.UUID;
|
||
|
|
||
|
import com.fasterxml.jackson.core.JsonFactory;
|
||
|
import com.fasterxml.jackson.core.JsonGenerator;
|
||
|
import org.junit.Test;
|
||
|
import org.junit.runner.RunWith;
|
||
|
import org.junit.runners.JUnit4;
|
||
|
|
||
|
/**
|
||
|
* Created by Alessia Bardi on 02/10/2017.
|
||
|
*
|
||
|
* @author Alessia Bardi
|
||
|
*/
|
||
|
@RunWith(JUnit4.class)
|
||
|
public class JacksonTest {
|
||
|
|
||
|
@Test
|
||
|
public void testJackson() throws IOException {
|
||
|
JsonFactory jsonFactory = new JsonFactory();
|
||
|
StringWriter sw = new StringWriter();
|
||
|
JsonGenerator jgen = jsonFactory.createGenerator(sw);
|
||
|
jgen.writeStartObject();
|
||
|
jgen.writeNumberField("id", 1);
|
||
|
jgen.writeArrayFieldStart("array");
|
||
|
jgen.writeStartObject();
|
||
|
jgen.writeStringField("itemName", "theName");
|
||
|
jgen.writeNumberField("owner", 3);
|
||
|
jgen.writeEndObject();
|
||
|
jgen.writeStartObject();
|
||
|
jgen.writeStringField("itemName", "theName2");
|
||
|
jgen.writeNumberField("owner", 8);
|
||
|
jgen.writeEndObject();
|
||
|
jgen.writeEndArray();
|
||
|
jgen.writeEndObject();
|
||
|
|
||
|
jgen.close();
|
||
|
//sw.flush();
|
||
|
System.out.println(sw.getBuffer().toString());
|
||
|
}
|
||
|
|
||
|
@Test
|
||
|
public void testJacksonEmbeddedObject() throws IOException {
|
||
|
JsonFactory jsonFactory = new JsonFactory();
|
||
|
StringWriter sw = new StringWriter();
|
||
|
JsonGenerator jgen = jsonFactory.createGenerator(sw);
|
||
|
jgen.writeStartObject();
|
||
|
jgen.writeNumberField("id", 1);
|
||
|
jgen.writeObjectFieldStart("embeddedObj");
|
||
|
jgen.writeStringField("itemName", "theName");
|
||
|
jgen.writeNumberField("owner", 3);
|
||
|
jgen.writeEndObject();
|
||
|
jgen.writeEndObject();
|
||
|
|
||
|
jgen.close();
|
||
|
System.out.println(sw.getBuffer().toString());
|
||
|
}
|
||
|
|
||
|
@Test
|
||
|
public void testHeader() throws IOException {
|
||
|
JsonFactory jsonFactory = new JsonFactory();
|
||
|
StringWriter sw = new StringWriter();
|
||
|
JsonGenerator jgen = jsonFactory.createGenerator(sw);
|
||
|
jgen.writeStartObject();
|
||
|
jgen.writeObjectFieldStart("header");
|
||
|
jgen.writeStringField("uuid", UUID.randomUUID().toString());
|
||
|
jgen.writeEndObject();
|
||
|
jgen.writeStringField("@class", "specificType");
|
||
|
jgen.writeArrayFieldStart("consistsOf");
|
||
|
jgen.writeEndArray();
|
||
|
jgen.writeEndObject();
|
||
|
|
||
|
jgen.close();
|
||
|
System.out.println(sw.getBuffer().toString());
|
||
|
}
|
||
|
}
|