Skip to content

Commit

Permalink
Added JsonSerializable interface
Browse files Browse the repository at this point in the history
  • Loading branch information
pbi-qfs committed Jun 1, 2017
1 parent 9700d31 commit e4e66e3
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,12 @@ public static JsonValue value(boolean value) {
}

/**
* Returns a JsonValue instance that represents the given object.
* Returns a JsonValue instance that represents the given object. If the object implements
* {@link JsonSerializable}, its {@link JsonSerializable#asJsonValue()} method is called and the
* result returned.
* <p>
* Converts null values to null literals and non-trivial objects to their string representation.
* Converts null values to null literals and non-trivial objects to their string representation.
*
* @param object
* the object to get a JSON representation for
* @return a JSON value that represents the given object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,20 @@
*/
public class JsonBuilder {
/**
* Creates a JsonValue from a Java object. Converts null values to null literals and non-trivial
* objects to their string representation.
*
* @param object The object to convert to json
* Creates a JsonValue from a Java object. If the object implements {@link JsonSerializable}, its
* {@link JsonSerializable#asJsonValue()} method is called and the result returned.
* <p>
* Converts null values to null literals and non-trivial objects to their string representation.
*
* @param object
* The object to convert to json
* @return The Java object as {@link JsonValue}
*/
public static JsonValue toJsonValue(final Object object) {
if (object == null) {
return Json.NULL;
} else if (object instanceof JsonSerializable) {
return ((JsonSerializable)object).asJsonValue();
} else if (object instanceof Boolean) {
return Json.value(((Boolean)object).booleanValue());
} else if (object instanceof Byte) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*******************************************************************************
* Copyright (c) 2017 EclipseSource.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package com.eclipsesource.json;

/**
* Objects, which implement this interface, can be directly serialized as {@link JsonValue}.
*/
public interface JsonSerializable {

/**
* Serializes the object into a {@link JsonValue}
*
* @return The object as json representation
*/
public JsonValue asJsonValue();
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.eclipsesource.json;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;

import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -110,6 +111,18 @@ public String toString() {
assertEquals("\"toString\"", value.toString());
}

@Test
public void callsAsJsonValueOfAJsonSerializeableObject() {
final JsonValue expected = Json.value(123);
JsonValue value = JsonBuilder.toJsonValue(new JsonSerializable() {

public JsonValue asJsonValue() {
return expected;
}
});
assertSame(expected, value);
}

@Test
public void convertsList() {
List<Object> list = new ArrayList<Object>();
Expand Down

0 comments on commit e4e66e3

Please sign in to comment.