Created
November 8, 2014 10:15
-
-
Save torgeir/7ffee14bf117217a6301 to your computer and use it in GitHub Desktop.
Poc json builder (java)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
// todo: generify!