Skip to content

Instantly share code, notes, and snippets.

@torgeir
Created November 8, 2014 10:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save torgeir/7ffee14bf117217a6301 to your computer and use it in GitHub Desktop.
Save torgeir/7ffee14bf117217a6301 to your computer and use it in GitHub Desktop.
Poc json builder (java)
package json;
import java.util.Arrays;
import java.util.List;
import static java.util.stream.Collectors.toList;
interface IBuildJson { String toJson(); }
interface IBuildArrays extends IBuildJson { }
interface IBuildProperties extends IBuildJson { }
class JsonBuilder implements IBuildJson {
private List<IBuildProperties> properties;
public JsonBuilder(IBuildProperties... properties) {
this.properties = Arrays.asList(properties);
}
@Override
public String toJson() {
List<String> jsonProperties = properties.stream()
.map(IBuildJson::toJson)
.collect(toList());
return "{" + String.join(",", jsonProperties) + "}";
}
}
class JsonStringBuilder implements IBuildProperties {
public final String name;
public final String value;
public JsonStringBuilder(String name, String value) {
this.name = name;
this.value = value;
}
@Override
public String toJson() {
return "\"" + name + "\": " + value;
}
}
class JsonIntegerBuilder implements IBuildProperties {
public final String name;
public final Integer value;
public JsonIntegerBuilder(String name, Integer value) {
this.name = name;
this.value = value;
}
@Override
public String toJson() {
return "\"" + name + "\": " + value;
}
}
class JsonBooleanBuilder implements IBuildProperties {
public final String name;
public final Boolean value;
public JsonBooleanBuilder(String name, Boolean value) {
this.name = name;
this.value = value;
}
@Override
public String toJson() {
return "\"" + name + "\": " + value;
}
}
class JsonPropertyBuilder implements IBuildProperties {
public final String name;
public final IBuildJson value;
public JsonPropertyBuilder(String name, IBuildJson value) {
this.name = name;
this.value = value;
}
@Override
public String toJson() {
return "\"" + name + "\": " + value.toJson();
}
}
class JsonArrayBuilder implements IBuildProperties {
public final String name;
public final IBuildJson json;
public JsonArrayBuilder(String name, IBuildJson value) {
this.name = name;
this.json = value;
}
@Override
public String toJson() {
return "\"" + name + "\": " + json.toJson();
}
}
class JsonArrayIntegerBuilder implements IBuildArrays {
public final List<Integer> is;
JsonArrayIntegerBuilder(Integer... is) {
this.is = Arrays.asList(is);
}
@Override
public String toJson() {
List<String> items = is.stream()
.map(Object::toString)
.collect(toList());
return "[" + String.join(",", items) + "]";
}
}
class JsonArrayJsonBuilder implements IBuildArrays {
public final List<IBuildJson> jsons;
JsonArrayJsonBuilder(IBuildJson... jsons) {
this.jsons = Arrays.asList(jsons);
}
@Override
public String toJson() {
List<String> items = jsons.stream()
.map(IBuildJson::toJson)
.collect(toList());
return "[" + String.join(",", items) + "]";
}
}
public class Builder {
static JsonBuilder json(IBuildProperties... property) {
return new JsonBuilder(property);
}
static JsonPropertyBuilder property(String name, IBuildJson json) {
return new JsonPropertyBuilder(name, json);
}
static JsonBooleanBuilder property(String name, Boolean value) {
return new JsonBooleanBuilder(name, value);
}
static JsonIntegerBuilder property(String name, Integer value) {
return new JsonIntegerBuilder(name, value);
}
static JsonStringBuilder property(String name, String value) {
return new JsonStringBuilder(name, value);
}
static JsonArrayBuilder property(String name, IBuildArrays array) {
return new JsonArrayBuilder(name, array);
}
static IBuildArrays array(Integer... is) {
return new JsonArrayIntegerBuilder(is);
}
static IBuildArrays array(JsonBuilder... jsons) {
return new JsonArrayJsonBuilder(jsons);
}
}
package json;
import static json.Builder.*;
public class BuilderTest {
public static void main(String[] args) {
IBuildJson nested = json(
property("nested-one", 1),
property("nested-array", array(1, 2, 3)));
IBuildJson json = json(
property("one", 1),
property("two", "2"),
property("three", json(
property("nested-json", nested),
property("nested-jsons", array(
json(
property("nested", nested),
property("another", 1)),
json(
property("not", false))))
)),
property("nested", nested));
System.out.println(json.toJson());
}
}
@torgeir
Copy link
Author

torgeir commented Nov 8, 2014

// todo: generify!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment