@Test public void testClientEntityJSONWithNull() throws ODataSerializerException { String expectedJson = "{\"@odata.type\":\"#test.testClientEntity\"," + "\"testInt32@odata.type\":\"Int32\"," + "\"testInt32\":12," + "\"testInt32Null@odata.type\":\"Int32\"" + ",\"testInt32Null\":null," + "\"testString@odata.type\":\"String\"," + "\"testString\":\"testString\"," + "\"testStringNull@odata.type\":\"String\"," + "\"testStringNull\":null}"; ODataClient odataClient = ODataClientFactory.getClient(); ClientObjectFactory objFactory = odataClient.getObjectFactory(); ClientEntity clientEntity = objFactory.newEntity(new FullQualifiedName("test", "testClientEntity")); clientEntity.getProperties().add( objFactory.newPrimitiveProperty( "testInt32", objFactory.newPrimitiveValueBuilder().buildInt32(12))); clientEntity.getProperties().add( objFactory.newPrimitiveProperty( "testInt32Null", objFactory.newPrimitiveValueBuilder().buildInt32(null))); clientEntity.getProperties().add( objFactory.newPrimitiveProperty( "testString", objFactory.newPrimitiveValueBuilder().buildString("testString"))); clientEntity.getProperties().add( objFactory.newPrimitiveProperty( "testStringNull", objFactory.newPrimitiveValueBuilder().buildString(null))); JsonSerializer jsonSerializer = new JsonSerializer(false, ContentType.JSON_FULL_METADATA); StringWriter writer = new StringWriter(); jsonSerializer.write(writer, odataClient.getBinder().getEntity(clientEntity)); assertThat(writer.toString(), is(expectedJson)); }
@Override public <T> void write(final Writer writer, final T obj) throws ODataSerializerException { try (final JsonGenerator json = new JsonFactory().createGenerator(writer)) { if (obj instanceof EntityCollection) { new JsonEntitySetSerializer(serverMode, contentType).doSerialize((EntityCollection) obj, json); } else if (obj instanceof Entity) { new JsonEntitySerializer(serverMode, contentType).doSerialize((Entity) obj, json); } else if (obj instanceof Property) { new JsonPropertySerializer(serverMode, contentType).doSerialize((Property) obj, json); } else if (obj instanceof Link) { link((Link) obj, json); } json.flush(); } catch (final IOException | EdmPrimitiveTypeException e) { throw new ODataSerializerException(e); } }
JsonSerializer implements ODataSerializer { @Override public <T> void write(final Writer writer, final T obj) throws ODataSerializerException { try (final JsonGenerator json = new JsonFactory().createGenerator(writer)) { if (obj instanceof EntityCollection) { new JsonEntitySetSerializer(serverMode, contentType).doSerialize((EntityCollection) obj, json); } else if (obj instanceof Entity) { new JsonEntitySerializer(serverMode, contentType).doSerialize((Entity) obj, json); } else if (obj instanceof Property) { new JsonPropertySerializer(serverMode, contentType).doSerialize((Property) obj, json); } else if (obj instanceof Link) { link((Link) obj, json); } json.flush(); } catch (final IOException | EdmPrimitiveTypeException e) { throw new ODataSerializerException(e); } } }
JsonSerializer implements ODataSerializer { @Override public <T> void write(final Writer writer, final T obj) throws ODataSerializerException { try (final JsonGenerator json = new JsonFactory().createGenerator(writer)) { if (obj instanceof EntityCollection) { new JsonEntitySetSerializer(serverMode, contentType).doSerialize((EntityCollection) obj, json); } else if (obj instanceof Entity) { new JsonEntitySerializer(serverMode, contentType).doSerialize((Entity) obj, json); } else if (obj instanceof Property) { new JsonPropertySerializer(serverMode, contentType).doSerialize((Property) obj, json); } else if (obj instanceof Link) { link((Link) obj, json); } json.flush(); } catch (final IOException | EdmPrimitiveTypeException e) { throw new ODataSerializerException(e); } } JsonSerializer(final boolean serverMode, final ContentType contentType); }
JsonSerializer implements ODataSerializer { @Override public <T> void write(final Writer writer, final T obj) throws ODataSerializerException { try (final JsonGenerator json = new JsonFactory().createGenerator(writer)) { if (obj instanceof EntityCollection) { new JsonEntitySetSerializer(serverMode, contentType).doSerialize((EntityCollection) obj, json); } else if (obj instanceof Entity) { new JsonEntitySerializer(serverMode, contentType).doSerialize((Entity) obj, json); } else if (obj instanceof Property) { new JsonPropertySerializer(serverMode, contentType).doSerialize((Property) obj, json); } else if (obj instanceof Link) { link((Link) obj, json); } json.flush(); } catch (final IOException | EdmPrimitiveTypeException e) { throw new ODataSerializerException(e); } } JsonSerializer(final boolean serverMode, final ContentType contentType); @Override void write(final Writer writer, final T obj); @SuppressWarnings("unchecked") @Override void write(final Writer writer, final ResWrap<T> container); }
JsonSerializer implements ODataSerializer { @Override public <T> void write(final Writer writer, final T obj) throws ODataSerializerException { try (final JsonGenerator json = new JsonFactory().createGenerator(writer)) { if (obj instanceof EntityCollection) { new JsonEntitySetSerializer(serverMode, contentType).doSerialize((EntityCollection) obj, json); } else if (obj instanceof Entity) { new JsonEntitySerializer(serverMode, contentType).doSerialize((Entity) obj, json); } else if (obj instanceof Property) { new JsonPropertySerializer(serverMode, contentType).doSerialize((Property) obj, json); } else if (obj instanceof Link) { link((Link) obj, json); } json.flush(); } catch (final IOException | EdmPrimitiveTypeException e) { throw new ODataSerializerException(e); } } JsonSerializer(final boolean serverMode, final ContentType contentType); @Override void write(final Writer writer, final T obj); @SuppressWarnings("unchecked") @Override void write(final Writer writer, final ResWrap<T> container);  }