Skip to content

Commit

Permalink
Move static factory methods from JsonValue to Json
Browse files Browse the repository at this point in the history
Same as with constants, the static methods on JsonValue are available
by the subclasses JsonArray and JsonObject by inheritance. This allows
for expressions like

    JsonArray.valueOf(23)

which would not return a JsonArray, as you could expect, but a
JsonValue.

Move static factory methods to the new util class Json and deprecate
the methods on JsonValue to prevent those cases.

This also results in shorter code:

    Json.value(23)

instead of

    JsonValue.valueOf(23)

Fix #52
  • Loading branch information
ralfstx committed Jul 11, 2015
1 parent 902897b commit db97bd7
Show file tree
Hide file tree
Showing 10 changed files with 252 additions and 135 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import java.io.IOException;

import com.eclipsesource.json.Json;
import com.eclipsesource.json.JsonValue;
import com.eclipsesource.json.performancetest.caliper.CaliperRunner;
import com.google.caliper.SimpleBenchmark;
Expand All @@ -32,13 +33,13 @@ public class IntVsLongBenchmark extends SimpleBenchmark {

public void timeValueOfInt(int reps) {
for (int i = 0; i < reps; i++) {
checkValue(JsonValue.valueOf(23));
checkValue(Json.value(23));
}
}

public void timeValueOfLong(int reps) {
for (int i = 0; i < reps; i++) {
checkValue(JsonValue.valueOf(23l));
checkValue(Json.value(23l));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
******************************************************************************/
package com.eclipsesource.json.performancetest.caliper;

import com.eclipsesource.json.Json;
import com.eclipsesource.json.JsonArray;
import com.eclipsesource.json.JsonObject;
import com.eclipsesource.json.JsonValue;
Expand Down Expand Up @@ -147,7 +148,7 @@ private static JsonValue extractBenchmarkName(JsonObject caliperResults) {

private static JsonValue extractSimpleName(JsonObject caliperResults) {
String name = caliperResults.get("run").asObject().get("benchmarkName").asString();
return JsonValue.valueOf(name.replaceFirst(".*\\.", ""));
return Json.value(name.replaceFirst(".*\\.", ""));
}

private static JsonValue extractTimestamp(JsonObject caliperResults) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,83 @@ private Json() {
*/
public static final JsonValue FALSE = new JsonLiteral("false");

/**
* Returns a JsonValue instance that represents the given <code>int</code> value.
*
* @param value
* the value to get a JSON representation for
* @return a JSON value that represents the given value
*/
public static JsonValue value(int value) {
return new JsonNumber(Integer.toString(value, 10));
}

/**
* Returns a JsonValue instance that represents the given <code>long</code> value.
*
* @param value
* the value to get a JSON representation for
* @return a JSON value that represents the given value
*/
public static JsonValue value(long value) {
return new JsonNumber(Long.toString(value, 10));
}

/**
* Returns a JsonValue instance that represents the given <code>float</code> value.
*
* @param value
* the value to get a JSON representation for
* @return a JSON value that represents the given 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");
}
return new JsonNumber(cutOffPointZero(Float.toString(value)));
}

/**
* Returns a JsonValue instance that represents the given <code>double</code> value.
*
* @param value
* the value to get a JSON representation for
* @return a JSON value that represents the given 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");
}
return new JsonNumber(cutOffPointZero(Double.toString(value)));
}

/**
* Returns a JsonValue instance that represents the given string.
*
* @param string
* the string to get a JSON representation for
* @return a JSON value that represents the given string
*/
public static JsonValue value(String string) {
return string == null ? NULL : new JsonString(string);
}

/**
* Returns a JsonValue instance that represents the given <code>boolean</code> value.
*
* @param value
* the value to get a JSON representation for
* @return a JSON value that represents the given value
*/
public static JsonValue value(boolean value) {
return value ? TRUE : FALSE;
}

private static String cutOffPointZero(String string) {
if (string.endsWith(".0")) {
return string.substring(0, string.length() - 2);
}
return string;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public static JsonArray unmodifiableArray(JsonArray array) {
* @return the array itself, to enable method chaining
*/
public JsonArray add(int value) {
values.add(valueOf(value));
values.add(Json.value(value));
return this;
}

Expand All @@ -167,7 +167,7 @@ public JsonArray add(int value) {
* @return the array itself, to enable method chaining
*/
public JsonArray add(long value) {
values.add(valueOf(value));
values.add(Json.value(value));
return this;
}

Expand All @@ -180,7 +180,7 @@ public JsonArray add(long value) {
* @return the array itself, to enable method chaining
*/
public JsonArray add(float value) {
values.add(valueOf(value));
values.add(Json.value(value));
return this;
}

Expand All @@ -193,7 +193,7 @@ public JsonArray add(float value) {
* @return the array itself, to enable method chaining
*/
public JsonArray add(double value) {
values.add(valueOf(value));
values.add(Json.value(value));
return this;
}

Expand All @@ -206,7 +206,7 @@ public JsonArray add(double value) {
* @return the array itself, to enable method chaining
*/
public JsonArray add(boolean value) {
values.add(valueOf(value));
values.add(Json.value(value));
return this;
}

Expand All @@ -218,7 +218,7 @@ public JsonArray add(boolean value) {
* @return the array itself, to enable method chaining
*/
public JsonArray add(String value) {
values.add(valueOf(value));
values.add(Json.value(value));
return this;
}

Expand Down Expand Up @@ -251,7 +251,7 @@ public JsonArray add(JsonValue value) {
* <code>index &gt;= size</code>
*/
public JsonArray set(int index, int value) {
values.set(index, valueOf(value));
values.set(index, Json.value(value));
return this;
}

Expand All @@ -269,7 +269,7 @@ public JsonArray set(int index, int value) {
* <code>index &gt;= size</code>
*/
public JsonArray set(int index, long value) {
values.set(index, valueOf(value));
values.set(index, Json.value(value));
return this;
}

Expand All @@ -287,7 +287,7 @@ public JsonArray set(int index, long value) {
* <code>index &gt;= size</code>
*/
public JsonArray set(int index, float value) {
values.set(index, valueOf(value));
values.set(index, Json.value(value));
return this;
}

Expand All @@ -305,7 +305,7 @@ public JsonArray set(int index, float value) {
* <code>index &gt;= size</code>
*/
public JsonArray set(int index, double value) {
values.set(index, valueOf(value));
values.set(index, Json.value(value));
return this;
}

Expand All @@ -323,7 +323,7 @@ public JsonArray set(int index, double value) {
* <code>index &gt;= size</code>
*/
public JsonArray set(int index, boolean value) {
values.set(index, valueOf(value));
values.set(index, Json.value(value));
return this;
}

Expand All @@ -341,7 +341,7 @@ public JsonArray set(int index, boolean value) {
* <code>index &gt;= size</code>
*/
public JsonArray set(int index, String value) {
values.set(index, valueOf(value));
values.set(index, Json.value(value));
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public static JsonObject unmodifiableObject(JsonObject object) {
* @return the object itself, to enable method chaining
*/
public JsonObject add(String name, int value) {
add(name, valueOf(value));
add(name, Json.value(value));
return this;
}

Expand All @@ -206,7 +206,7 @@ public JsonObject add(String name, int value) {
* @return the object itself, to enable method chaining
*/
public JsonObject add(String name, long value) {
add(name, valueOf(value));
add(name, Json.value(value));
return this;
}

Expand All @@ -229,7 +229,7 @@ public JsonObject add(String name, long value) {
* @return the object itself, to enable method chaining
*/
public JsonObject add(String name, float value) {
add(name, valueOf(value));
add(name, Json.value(value));
return this;
}

Expand All @@ -252,7 +252,7 @@ public JsonObject add(String name, float value) {
* @return the object itself, to enable method chaining
*/
public JsonObject add(String name, double value) {
add(name, valueOf(value));
add(name, Json.value(value));
return this;
}

Expand All @@ -275,7 +275,7 @@ public JsonObject add(String name, double value) {
* @return the object itself, to enable method chaining
*/
public JsonObject add(String name, boolean value) {
add(name, valueOf(value));
add(name, Json.value(value));
return this;
}

Expand All @@ -298,7 +298,7 @@ public JsonObject add(String name, boolean value) {
* @return the object itself, to enable method chaining
*/
public JsonObject add(String name, String value) {
add(name, valueOf(value));
add(name, Json.value(value));
return this;
}

Expand Down Expand Up @@ -351,7 +351,7 @@ public JsonObject add(String name, JsonValue value) {
* @return the object itself, to enable method chaining
*/
public JsonObject set(String name, int value) {
set(name, valueOf(value));
set(name, Json.value(value));
return this;
}

Expand All @@ -373,7 +373,7 @@ public JsonObject set(String name, int value) {
* @return the object itself, to enable method chaining
*/
public JsonObject set(String name, long value) {
set(name, valueOf(value));
set(name, Json.value(value));
return this;
}

Expand All @@ -395,7 +395,7 @@ public JsonObject set(String name, long value) {
* @return the object itself, to enable method chaining
*/
public JsonObject set(String name, float value) {
set(name, valueOf(value));
set(name, Json.value(value));
return this;
}

Expand All @@ -417,7 +417,7 @@ public JsonObject set(String name, float value) {
* @return the object itself, to enable method chaining
*/
public JsonObject set(String name, double value) {
set(name, valueOf(value));
set(name, Json.value(value));
return this;
}

Expand All @@ -439,7 +439,7 @@ public JsonObject set(String name, double value) {
* @return the object itself, to enable method chaining
*/
public JsonObject set(String name, boolean value) {
set(name, valueOf(value));
set(name, Json.value(value));
return this;
}

Expand All @@ -461,7 +461,7 @@ public JsonObject set(String name, boolean value) {
* @return the object itself, to enable method chaining
*/
public JsonObject set(String name, String value) {
set(name, valueOf(value));
set(name, Json.value(value));
return this;
}

Expand Down
Loading

0 comments on commit db97bd7

Please sign in to comment.