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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
bin/
target/
.history/
*.iml
.idea/
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@
*/
public final class Json {

// String constants
private static final String VALUES_IS_NULL = "values is null";
private static final String STRING_IS_NULL = "string is null";
private static final String READER_IS_NULL = "reader is null";
private static final String INFINITE_AND_NAN = "Infinite and NaN values not permitted in JSON";

private Json() {
// not meant to be instantiated
}
Expand Down Expand Up @@ -103,7 +109,7 @@ public static JsonValue value(long value) {
*/
public static JsonValue value(float value) {
if (Float.isInfinite(value) || Float.isNaN(value)) {
throw new IllegalArgumentException("Infinite and NaN values not permitted in JSON");
throw new IllegalArgumentException(INFINITE_AND_NAN);
}
return new JsonNumber(cutOffPointZero(Float.toString(value)));
}
Expand All @@ -117,7 +123,7 @@ public static JsonValue value(float value) {
*/
public static JsonValue value(double value) {
if (Double.isInfinite(value) || Double.isNaN(value)) {
throw new IllegalArgumentException("Infinite and NaN values not permitted in JSON");
throw new IllegalArgumentException(INFINITE_AND_NAN);
}
return new JsonNumber(cutOffPointZero(Double.toString(value)));
}
Expand Down Expand Up @@ -164,7 +170,7 @@ public static JsonArray array() {
*/
public static JsonArray array(int... values) {
if (values == null) {
throw new NullPointerException("values is null");
throw new NullPointerException(VALUES_IS_NULL);
}
JsonArray array = new JsonArray();
for (int value : values) {
Expand All @@ -183,7 +189,7 @@ public static JsonArray array(int... values) {
*/
public static JsonArray array(long... values) {
if (values == null) {
throw new NullPointerException("values is null");
throw new NullPointerException(VALUES_IS_NULL);
}
JsonArray array = new JsonArray();
for (long value : values) {
Expand All @@ -202,7 +208,7 @@ public static JsonArray array(long... values) {
*/
public static JsonArray array(float... values) {
if (values == null) {
throw new NullPointerException("values is null");
throw new NullPointerException(VALUES_IS_NULL);
}
JsonArray array = new JsonArray();
for (float value : values) {
Expand All @@ -221,7 +227,7 @@ public static JsonArray array(float... values) {
*/
public static JsonArray array(double... values) {
if (values == null) {
throw new NullPointerException("values is null");
throw new NullPointerException(VALUES_IS_NULL);
}
JsonArray array = new JsonArray();
for (double value : values) {
Expand All @@ -240,7 +246,7 @@ public static JsonArray array(double... values) {
*/
public static JsonArray array(boolean... values) {
if (values == null) {
throw new NullPointerException("values is null");
throw new NullPointerException(VALUES_IS_NULL);
}
JsonArray array = new JsonArray();
for (boolean value : values) {
Expand All @@ -258,7 +264,7 @@ public static JsonArray array(boolean... values) {
*/
public static JsonArray array(String... strings) {
if (strings == null) {
throw new NullPointerException("values is null");
throw new NullPointerException(VALUES_IS_NULL);
}
JsonArray array = new JsonArray();
for (String value : strings) {
Expand Down Expand Up @@ -289,7 +295,7 @@ public static JsonObject object() {
*/
public static JsonValue parse(String string) {
if (string == null) {
throw new NullPointerException("string is null");
throw new NullPointerException(STRING_IS_NULL);
}
DefaultHandler handler = new DefaultHandler();
new JsonParser(handler).parse(string);
Expand All @@ -314,7 +320,7 @@ public static JsonValue parse(String string) {
*/
public static JsonValue parse(Reader reader) throws IOException {
if (reader == null) {
throw new NullPointerException("reader is null");
throw new NullPointerException(READER_IS_NULL);
}
DefaultHandler handler = new DefaultHandler();
new JsonParser(handler).parse(reader);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@
@SuppressWarnings("serial") // use default serial UID
public class JsonArray extends JsonValue implements Iterable<JsonValue> {

// String constants
private static final String ARRAY_IS_NULL = "array is null";

private final List<JsonValue> values;

/**
Expand All @@ -83,7 +86,7 @@ public JsonArray(JsonArray array) {

private JsonArray(JsonArray array, boolean unmodifiable) {
if (array == null) {
throw new NullPointerException("array is null");
throw new NullPointerException(ARRAY_IS_NULL);
}
if (unmodifiable) {
values = Collections.unmodifiableList(array.values);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@
@SuppressWarnings("serial") // use default serial UID
public class JsonObject extends JsonValue implements Iterable<Member> {

// String constants
private static final String OBJECT_IS_NULL = "object is null";
private static final String NAME_IS_NULL = "name is null";
private static final String VALUE_IS_NULL = "value is null";

private final List<String> names;
private final List<JsonValue> values;
private transient HashIndexTable table;
Expand All @@ -97,7 +102,7 @@ public JsonObject(JsonObject object) {

private JsonObject(JsonObject object, boolean unmodifiable) {
if (object == null) {
throw new NullPointerException("object is null");
throw new NullPointerException(OBJECT_IS_NULL);
}
if (unmodifiable) {
names = Collections.unmodifiableList(object.names);
Expand Down Expand Up @@ -326,10 +331,10 @@ public JsonObject add(String name, String value) {
*/
public JsonObject add(String name, JsonValue value) {
if (name == null) {
throw new NullPointerException("name is null");
throw new NullPointerException(NAME_IS_NULL);
}
if (value == null) {
throw new NullPointerException("value is null");
throw new NullPointerException(VALUE_IS_NULL);
}
table.add(name, names.size());
names.add(name);
Expand Down Expand Up @@ -487,10 +492,10 @@ public JsonObject set(String name, String value) {
*/
public JsonObject set(String name, JsonValue value) {
if (name == null) {
throw new NullPointerException("name is null");
throw new NullPointerException(NAME_IS_NULL);
}
if (value == null) {
throw new NullPointerException("value is null");
throw new NullPointerException(VALUE_IS_NULL);
}
int index = indexOf(name);
if (index != -1) {
Expand All @@ -514,7 +519,7 @@ public JsonObject set(String name, JsonValue value) {
*/
public JsonObject remove(String name) {
if (name == null) {
throw new NullPointerException("name is null");
throw new NullPointerException(NAME_IS_NULL);
}
int index = indexOf(name);
if (index != -1) {
Expand Down Expand Up @@ -549,7 +554,7 @@ public boolean contains(String name) {
*/
public JsonObject merge(JsonObject object) {
if (object == null) {
throw new NullPointerException("object is null");
throw new NullPointerException(OBJECT_IS_NULL);
}
for (Member member : object) {
this.set(member.name, member.value);
Expand All @@ -568,7 +573,7 @@ public JsonObject merge(JsonObject object) {
*/
public JsonValue get(String name) {
if (name == null) {
throw new NullPointerException("name is null");
throw new NullPointerException(NAME_IS_NULL);
}
int index = indexOf(name);
return index != -1 ? values.get(index) : null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@
@SuppressWarnings("serial") // use default serial UID
public abstract class JsonValue implements Serializable {

// String constants
private static final String NOT_A_NUMBER = "Not a number: ";
private static final String NOT_A_STRING = "Not a string: ";
private static final String NOT_A_BOOLEAN = "Not a boolean: ";
private static final String NOT_AN_OBJECT = "Not an object: ";
private static final String NOT_AN_ARRAY = "Not an array: ";

/**
* Represents the JSON literal <code>true</code>.
* @deprecated Use <code>Json.TRUE</code> instead
Expand Down Expand Up @@ -287,7 +294,7 @@ public boolean isNull() {
* if this value is not a JSON object
*/
public JsonObject asObject() {
throw new UnsupportedOperationException("Not an object: " + toString());
throw new UnsupportedOperationException(NOT_AN_OBJECT + toString());
}

/**
Expand All @@ -299,7 +306,7 @@ public JsonObject asObject() {
* if this value is not a JSON array
*/
public JsonArray asArray() {
throw new UnsupportedOperationException("Not an array: " + toString());
throw new UnsupportedOperationException(NOT_AN_ARRAY + toString());
}

/**
Expand All @@ -318,7 +325,7 @@ public JsonArray asArray() {
* if this JSON number can not be interpreted as <code>int</code> value
*/
public int asInt() {
throw new UnsupportedOperationException("Not a number: " + toString());
throw new UnsupportedOperationException(NOT_A_NUMBER + toString());
}

/**
Expand All @@ -337,7 +344,7 @@ public int asInt() {
* if this JSON number can not be interpreted as <code>long</code> value
*/
public long asLong() {
throw new UnsupportedOperationException("Not a number: " + toString());
throw new UnsupportedOperationException(NOT_A_NUMBER + toString());
}

/**
Expand All @@ -353,7 +360,7 @@ public long asLong() {
* if this value is not a JSON number
*/
public float asFloat() {
throw new UnsupportedOperationException("Not a number: " + toString());
throw new UnsupportedOperationException(NOT_A_NUMBER + toString());
}

/**
Expand All @@ -369,7 +376,7 @@ public float asFloat() {
* if this value is not a JSON number
*/
public double asDouble() {
throw new UnsupportedOperationException("Not a number: " + toString());
throw new UnsupportedOperationException(NOT_A_NUMBER + toString());
}

/**
Expand All @@ -381,7 +388,7 @@ public double asDouble() {
* if this value is not a JSON string
*/
public String asString() {
throw new UnsupportedOperationException("Not a string: " + toString());
throw new UnsupportedOperationException(NOT_A_STRING + toString());
}

/**
Expand All @@ -393,7 +400,7 @@ public String asString() {
* if this value is neither <code>true</code> or <code>false</code>
*/
public boolean asBoolean() {
throw new UnsupportedOperationException("Not a boolean: " + toString());
throw new UnsupportedOperationException(NOT_A_BOOLEAN + toString());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@
package com.eclipsesource.json;


import java.io.Serializable;

/**
* An immutable object that represents a location in the parsed text.
*/
public class Location {
@SuppressWarnings("serial") // use default serial UID
public class Location implements Serializable {

/**
* The absolute character index, starting at 0.
Expand Down