Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,22 @@ JsonValue name = Json.value("Alice");
JsonValue points = Json.value(23);
```

And there are methods for creating empty arrays and objects as well.
There is also a variant of `value()` to create JSON values directly form any Java object:

```java
Set<Object> javaValues = new HashSet<Object>();
javaValues.add("me");
javaValues.add(3.141);
javaValues.add(new Date());
JsonValue jsonValues = Json.value(javaValues);
```

If you want to control the serialization of your own objects, implement the
`JsonSerializable` interface, which is respected by the `value()` method.
Any other object, which cannot be directly mapped to a JSON value is converted to a string using the
`toString()`method of the object. Circular references are not resolved.

There are methods for creating empty arrays and objects as well.
Use these together with `add` to create data structures:

```java
Expand All @@ -146,7 +161,7 @@ You can also create JSON arrays conveniently from Java arrays such as `String[]`

```java
String[] javaNames = {"Alice", "Bob"};
JsonArray jsonNames = Json.array(names);
JsonArray jsonNames = Json.array(javaNames);
```

### Modify JSON arrays and objects
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@
* String[] names = ...
* JsonArray array = Json.array(names);
* </pre>
* <p>
* To create a JSON array from any given Java object, you can use the <code>value(object)</code>
* method:
* </p>
* <pre>
* Object object = ...
* JsonValue json = Json.value(object);
* </pre>
*/
public final class Json {

Expand Down Expand Up @@ -144,6 +152,21 @@ public static JsonValue value(boolean value) {
return value ? TRUE : FALSE;
}

/**
* 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.
*
* @param object
* the object to get a JSON representation for
* @return a JSON value that represents the given object
*/
public static JsonValue value(Object object) {
return JsonBuilder.toJsonValue(object);
}

/**
* Creates a new empty JsonArray. This is equivalent to creating a new JsonArray using the
* constructor.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*******************************************************************************
* Copyright (c) 2013, 2015 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;

import java.lang.reflect.Array;

import java.util.Map;
import java.util.Map.Entry;


/**
* A simple builder which allows to create a JsonValue from a java object. <b> As this is part of
* the minimal-json library, it is a minimal converter, and therefore does <b>not</b> resolve cyclic
* structures.
*
* @author Pascal Bihler
*/
public class JsonBuilder {
/**
* 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 JsonValue) {
return (JsonValue) object;
} 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) {
return Json.value(((Byte)object).byteValue());
} else if (object instanceof Short) {
return Json.value(((Short)object).shortValue());
} else if (object instanceof Integer) {
return Json.value(((Integer)object).intValue());
} else if (object instanceof Long) {
return Json.value(((Long)object).longValue());
} else if (object instanceof Float) {
return Json.value(((Float)object).floatValue());
} else if (object instanceof Double) {
return Json.value(((Double)object).doubleValue());
} else if (object.getClass().isArray()) {
return arrayToJsonValue(object);
} else if (object instanceof Iterable) {
return iterableToJsonValue((Iterable<?>)object);
} else if (object instanceof Map) {
return mapToJsonValue((Map<?, ?>)object);
} else {
return Json.value(String.valueOf(object));
}
}

/**
* Creates a JsonArray from a collection object.
*/
static JsonArray iterableToJsonValue(final Iterable<?> collection) {
final JsonArray array = new JsonArray();
for (final Object element : collection) {
array.add(toJsonValue(element));
}
return array;
}

/**
* Creates a JsonObject from a Java Map
*/
static JsonObject mapToJsonValue(final Map<?, ?> map) {
final JsonObject object = new JsonObject();
for (final Entry<?, ?> entry : map.entrySet()) {
object.add(String.valueOf(entry.getKey()), toJsonValue(entry.getValue()));
}
return object;
}

/**
* Creates a JsonArray from an array.
*/
static JsonValue arrayToJsonValue(final Object inputArray) {
final JsonArray array = new JsonArray();
final int arrayLength = Array.getLength(inputArray);
for (int i = 0; i < arrayLength; i++) {
final Object element = Array.get(inputArray, i);
array.add(toJsonValue(element));
}
return array;
}
}
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();
}
Loading