Search in sources :

Example 6 with Rfc3339DateJsonAdapter

use of com.squareup.moshi.adapters.Rfc3339DateJsonAdapter in project axis-axis2-java-core by apache.

the class MoshiXMLStreamWriterTest method testMoshiXMLStreamWriter.

@Test
public void testMoshiXMLStreamWriter() throws Exception {
    jsonString = "{\"response\":{\"return\":{\"name\":\"kate\",\"age\":\"35\",\"gender\":\"female\"}}}";
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Moshi moshi = new Moshi.Builder().add(Date.class, new Rfc3339DateJsonAdapter()).build();
    JsonAdapter<Object> adapter = moshi.adapter(Object.class);
    BufferedSink sink = Okio.buffer(Okio.sink(baos));
    JsonWriter jsonWriter = JsonWriter.of(sink);
    String fileName = "test-resources/custom_schema/testSchema_1.xsd";
    InputStream is = new FileInputStream(fileName);
    XmlSchemaCollection schemaCol = new XmlSchemaCollection();
    XmlSchema schema = schemaCol.read(new StreamSource(is));
    List<XmlSchema> schemaList = new ArrayList<XmlSchema>();
    schemaList.add(schema);
    QName elementQName = new QName("http://www.w3schools.com", "response");
    ConfigurationContext configCtxt = new ConfigurationContext(new AxisConfiguration());
    MoshiXMLStreamWriter moshiXMLStreamWriter = new MoshiXMLStreamWriter(jsonWriter, elementQName, schemaList, configCtxt);
    OMElement omElement = getResponseOMElement();
    moshiXMLStreamWriter.writeStartDocument();
    omElement.serialize(moshiXMLStreamWriter);
    moshiXMLStreamWriter.writeEndDocument();
    String actualString = baos.toString();
    sink.close();
    Assert.assertEquals(jsonString, actualString);
}
Also used : ConfigurationContext(org.apache.axis2.context.ConfigurationContext) AxisConfiguration(org.apache.axis2.engine.AxisConfiguration) Moshi(com.squareup.moshi.Moshi) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) QName(javax.xml.namespace.QName) Rfc3339DateJsonAdapter(com.squareup.moshi.adapters.Rfc3339DateJsonAdapter) StreamSource(javax.xml.transform.stream.StreamSource) ArrayList(java.util.ArrayList) BufferedSink(okio.BufferedSink) OMElement(org.apache.axiom.om.OMElement) ByteArrayOutputStream(java.io.ByteArrayOutputStream) JsonWriter(com.squareup.moshi.JsonWriter) XmlSchemaCollection(org.apache.ws.commons.schema.XmlSchemaCollection) Date(java.util.Date) FileInputStream(java.io.FileInputStream) XmlSchema(org.apache.ws.commons.schema.XmlSchema) Test(org.junit.Test)

Example 7 with Rfc3339DateJsonAdapter

use of com.squareup.moshi.adapters.Rfc3339DateJsonAdapter in project moshi by square.

the class IncludeNullsForOneType method run.

public void run() throws Exception {
    Moshi moshi = new Moshi.Builder().add(Date.class, new Rfc3339DateJsonAdapter()).add(new TournamentWithNullsAdapter()).build();
    JsonAdapter<Tournament> tournamentAdapter = moshi.adapter(Tournament.class);
    // Moshi normally skips nulls, but with our adapter registered they are emitted.
    Tournament withNulls = new Tournament("Waterloo Classic", null, null);
    System.out.println(tournamentAdapter.toJson(withNulls));
}
Also used : Moshi(com.squareup.moshi.Moshi) Rfc3339DateJsonAdapter(com.squareup.moshi.adapters.Rfc3339DateJsonAdapter) Tournament(com.squareup.moshi.recipes.models.Tournament)

Aggregations

Moshi (com.squareup.moshi.Moshi)7 Rfc3339DateJsonAdapter (com.squareup.moshi.adapters.Rfc3339DateJsonAdapter)7 Date (java.util.Date)6 JsonWriter (com.squareup.moshi.JsonWriter)3 Tournament (com.squareup.moshi.recipes.models.Tournament)2 IOException (java.io.IOException)2 Type (java.lang.reflect.Type)2 ArrayList (java.util.ArrayList)2 QName (javax.xml.namespace.QName)2 BufferedSink (okio.BufferedSink)2 OMElement (org.apache.axiom.om.OMElement)2 Test (org.junit.Test)2 ProfilerConfiguration (com.microsoft.applicationinsights.profiler.ProfilerConfiguration)1 JsonAdapter (com.squareup.moshi.JsonAdapter)1 JsonReader (com.squareup.moshi.JsonReader)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Annotation (java.lang.annotation.Annotation)1