private JSONObject serializeObject(final SerializableEntity object) { if (null == object) { return new JSONObject(); } final JsonSerializer serializer = new JsonSerializer(this.getContext(), null != this.propertyOrderArray); object.serialize(serializer); return serializer.getObject(); }
@Test public void responseCanBeSerialized() { // Arrange: final JsonSerializer serializer = new JsonSerializer(); final ErrorResponse response = new ErrorResponse(new TimeInstant(18), "badness", 500); // Act: response.serialize(serializer); final JSONObject jsonObject = serializer.getObject(); // Assert: Assert.assertThat(jsonObject.get("timeStamp"), IsEqual.equalTo(18)); Assert.assertThat(jsonObject.get("error"), IsEqual.equalTo("Internal Server Error")); Assert.assertThat(jsonObject.get("message"), IsEqual.equalTo("badness")); Assert.assertThat(jsonObject.get("status"), IsEqual.equalTo(500)); }
@Test public void descriptorCanBeSerialized() { // Arrange: final File file = new File(Paths.get(TEST_FILE_DIRECTORY.toString(), "blah").toString(), "foo.bar"); final TEntityFileDescriptor descriptor = this.createDescriptor(file); // Act: final JSONObject jsonObject = JsonSerializer.serializeToJson(descriptor); // Assert: Assert.assertThat(jsonObject.size(), IsEqual.equalTo(2)); Assert.assertThat(jsonObject.get(descriptor.getName().getLabel()), IsEqual.equalTo("foo")); Assert.assertThat( jsonObject.get("location"), IsEqual.equalTo(Paths.get(TEST_FILE_DIRECTORY.toString(), "blah", "foo.bar").toString())); }
/** * Helper function that serializes a SerializableEntity to a JSON object. * * @param entity The entity to serialize. * @return The resulting JSON object. */ public static JSONObject serializeToJson(final SerializableEntity entity) { final JsonSerializer serializer = new JsonSerializer(); entity.serialize(serializer); return serializer.getObject(); }