Last active
January 10, 2018 12:49
-
-
Save hanbei/60bc254db7614e1652c4 to your computer and use it in GitHub Desktop.
JsonObjectBuilder
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 com.foxydeal.fdcinput; | |
/** | |
* Created by fschulz on 17.12.2015. | |
*/ | |
public class JsonObjectBuilder { | |
public static final String QUOTE = "\""; | |
private StringBuilder jsonString; | |
private int propertyCounter = 0; | |
public JsonObjectBuilder() { | |
this.jsonString = new StringBuilder(); | |
jsonString.append("{"); | |
} | |
public static JsonObjectBuilder json() { | |
return new JsonObjectBuilder(); | |
} | |
public String end() { | |
String s = jsonString.append("}").toString(); | |
jsonString = new StringBuilder(); | |
return s; | |
} | |
public JsonObjectBuilder startObject(String fieldName) { | |
addCommaIfMoreThanOneElement(); | |
jsonString.append(QUOTE).append(fieldName).append(QUOTE).append(":{"); | |
return this; | |
} | |
public JsonObjectBuilder endObject() { | |
jsonString.append("}"); | |
return this; | |
} | |
public JsonObjectBuilder property(String fieldName, String value) { | |
addCommaIfMoreThanOneElement(); | |
jsonString.append(QUOTE).append(fieldName).append(QUOTE).append(":").append(QUOTE).append(value).append(QUOTE); | |
return this; | |
} | |
public JsonObjectBuilder property(String fieldName, Number value) { | |
addCommaIfMoreThanOneElement(); | |
jsonString.append(QUOTE).append(fieldName).append(QUOTE).append(":").append(value); | |
return this; | |
} | |
private void addCommaIfMoreThanOneElement() { | |
if (propertyCounter > 0) { | |
jsonString.append(","); | |
} | |
propertyCounter++; | |
} | |
public JsonArrayBuilder array(String fieldName) { | |
return new JsonArrayBuilder(this, fieldName); | |
} | |
public class JsonArrayBuilder { | |
private JsonObjectBuilder jsonObjectBuilder; | |
int counter = 0; | |
public JsonArrayBuilder(JsonObjectBuilder jsonObjectBuilder, String arrayName) { | |
this.jsonObjectBuilder = jsonObjectBuilder; | |
jsonObjectBuilder.jsonString.append(QUOTE).append(arrayName).append(QUOTE).append(":["); | |
} | |
public JsonArrayBuilder add(Number number) { | |
addCommaIfMoreThanOneElement(); | |
jsonString.append(number); | |
return this; | |
} | |
public JsonArrayBuilder add(String string) { | |
addCommaIfMoreThanOneElement(); | |
jsonString.append(QUOTE).append(string).append(QUOTE); | |
return this; | |
} | |
private void addCommaIfMoreThanOneElement() { | |
if (counter > 0) { | |
jsonString.append(","); | |
} | |
counter++; | |
} | |
public JsonObjectBuilder arrayEnd() { | |
jsonString.append("]"); | |
return jsonObjectBuilder; | |
} | |
} | |
} |
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 com.foxydeal.fdcinput; | |
import org.junit.Before; | |
import org.junit.Test; | |
import static com.foxydeal.fdcinput.JsonObjectBuilder.json; | |
import static org.hamcrest.core.Is.is; | |
import static org.junit.Assert.assertThat; | |
/** | |
* Created by fschulz on 17.12.2015. | |
*/ | |
public class JsonObjectBuilderTest { | |
@Before | |
public void setUp() throws Exception { | |
} | |
@Test | |
public void testEmptyJson() { | |
String json = json().end(); | |
assertThat(json, is("{}")); | |
} | |
@Test | |
public void testSingleStringProperty() { | |
String json = json().property("test", "test").end(); | |
assertThat(json, is("{\"test\":\"test\"}")); | |
} | |
@Test | |
public void testMultipleProperties() { | |
String json = json() | |
.property("test", "test") | |
.property("test2", 3) | |
.property("test3", 3.3) | |
.end(); | |
assertThat(json, is("{\"test\":\"test\",\"test2\":3,\"test3\":3.3}")); | |
} | |
@Test | |
public void testSingleDoubleProperty() { | |
String json = json().property("test", 1.12355123).end(); | |
assertThat(json, is("{\"test\":1.12355123}")); | |
} | |
@Test | |
public void testSingleIntegerProperty() { | |
String json = json().property("test", 1).end(); | |
assertThat(json, is("{\"test\":1}")); | |
} | |
@Test | |
public void testArrayIntegerProperty() { | |
String json = json().array("array").add(1).add(2).add(3).arrayEnd().end(); | |
assertThat(json, is("{\"array\":[1,2,3]}")); | |
} | |
@Test | |
public void testArrayDoubleProperty() { | |
String json = json().array("array").add(1.1).add(2.2).add(3.3).arrayEnd().end(); | |
assertThat(json, is("{\"array\":[1.1,2.2,3.3]}")); | |
} | |
@Test | |
public void testArrayStringProperty() { | |
String json = json().array("array").add("bla1").add("bla2").add("bla3").arrayEnd().end(); | |
assertThat(json, is("{\"array\":[\"bla1\",\"bla2\",\"bla3\"]}")); | |
} | |
@Test | |
public void testEmptySubObject() { | |
String json = json().startObject("test").endObject().end(); | |
assertThat(json, is("{\"test\":{}}")); | |
} | |
@Test | |
public void testSubObject() { | |
String json = json().startObject("test").property("inner_test", "bla").endObject().end(); | |
assertThat(json, is("{\"test\":{\"inner_test\":\"bla\"}}")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment