fix begin/endMap collections, array element separation moved to beginMap, drop unnecessary methods
This commit is contained in:
parent
9953d28bac
commit
cce92c775b
2 changed files with 42 additions and 46 deletions
|
@ -52,6 +52,9 @@ public class JsonBuilder implements Builder {
|
|||
|
||||
@Override
|
||||
public Builder beginMap() throws IOException {
|
||||
if (state.structure == Structure.COLLECTION) {
|
||||
beginArrayValue();
|
||||
}
|
||||
this.state = new State(state, state.level + 1, Structure.MAP, true);
|
||||
appendable.append('{');
|
||||
return this;
|
||||
|
@ -70,9 +73,6 @@ public class JsonBuilder implements Builder {
|
|||
@Override
|
||||
public Builder buildMap(Map<String, Object> map) throws IOException {
|
||||
Objects.requireNonNull(map);
|
||||
if (state.structure == Structure.COLLECTION) {
|
||||
beginArrayValue(map);
|
||||
}
|
||||
boolean wrap = state.structure != Structure.MAP;
|
||||
if (wrap) {
|
||||
beginMap();
|
||||
|
@ -88,9 +88,6 @@ public class JsonBuilder implements Builder {
|
|||
if (wrap) {
|
||||
endMap();
|
||||
}
|
||||
if (state.structure == Structure.COLLECTION) {
|
||||
endArrayValue(map);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
|
@ -119,10 +116,8 @@ public class JsonBuilder implements Builder {
|
|||
buildCollection((Collection<Object>) object);
|
||||
return this;
|
||||
}
|
||||
if (state.structure == Structure.MAP || state.structure == Structure.KEY) {
|
||||
beginValue(object);
|
||||
} else if (state.structure == Structure.COLLECTION) {
|
||||
beginArrayValue(object);
|
||||
if (state.structure == Structure.COLLECTION) {
|
||||
beginArrayValue();
|
||||
}
|
||||
if (object == null) {
|
||||
buildNull();
|
||||
|
@ -151,18 +146,13 @@ public class JsonBuilder implements Builder {
|
|||
} else {
|
||||
throw new IllegalArgumentException("unable to write object class " + object.getClass());
|
||||
}
|
||||
if (state.structure == Structure.MAP || state.structure == Structure.KEY) {
|
||||
endValue(object);
|
||||
} else if (state.structure == Structure.COLLECTION) {
|
||||
endArrayValue(object);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder buildKey(CharSequence string) throws IOException {
|
||||
if (state.structure == Structure.COLLECTION) {
|
||||
beginArrayValue(string);
|
||||
beginArrayValue();
|
||||
} else if (state.structure == Structure.MAP || state.structure == Structure.KEY) {
|
||||
beginKey(string != null ? string.toString() : null);
|
||||
}
|
||||
|
@ -176,34 +166,19 @@ public class JsonBuilder implements Builder {
|
|||
|
||||
@Override
|
||||
public Builder buildNull() throws IOException {
|
||||
if (state.structure == Structure.MAP || state.structure == Structure.KEY) {
|
||||
beginValue(null);
|
||||
} else if (state.structure == Structure.COLLECTION) {
|
||||
beginArrayValue(null);
|
||||
if (state.structure == Structure.COLLECTION) {
|
||||
beginArrayValue();
|
||||
}
|
||||
buildString("null", false);
|
||||
if (state.structure == Structure.MAP || state.structure == Structure.KEY) {
|
||||
endValue(null);
|
||||
} else if (state.structure == Structure.COLLECTION) {
|
||||
endArrayValue(null);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder copy(Builder builder) throws IOException {
|
||||
if (state.structure == Structure.MAP || state.structure == Structure.KEY) {
|
||||
beginValue(null);
|
||||
} else if (state.structure == Structure.COLLECTION) {
|
||||
beginArrayValue(null);
|
||||
if (state.structure == Structure.COLLECTION) {
|
||||
beginArrayValue();
|
||||
}
|
||||
appendable.append(builder.build());
|
||||
if (state.structure == Structure.MAP || state.structure == Structure.KEY) {
|
||||
endValue(null);
|
||||
}
|
||||
if (state.structure == Structure.COLLECTION) {
|
||||
endArrayValue(null);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
|
@ -224,13 +199,7 @@ public class JsonBuilder implements Builder {
|
|||
appendable.append(":");
|
||||
}
|
||||
|
||||
private void beginValue(Object v) {
|
||||
}
|
||||
|
||||
private void endValue(Object v) {
|
||||
}
|
||||
|
||||
private void beginArrayValue(Object v) throws IOException {
|
||||
private void beginArrayValue() throws IOException {
|
||||
if (state.first) {
|
||||
state.first = false;
|
||||
} else {
|
||||
|
@ -238,9 +207,6 @@ public class JsonBuilder implements Builder {
|
|||
}
|
||||
}
|
||||
|
||||
private void endArrayValue(Object v) {
|
||||
}
|
||||
|
||||
private void buildBoolean(boolean bool) throws IOException {
|
||||
buildString(bool ? "true" : "false", false);
|
||||
}
|
||||
|
|
|
@ -8,6 +8,7 @@ import org.xbib.datastructures.json.tiny.StreamParser;
|
|||
import java.io.IOException;
|
||||
import java.io.StringReader;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
|
@ -233,9 +234,38 @@ public class JsonBuilderTest {
|
|||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void testJsonToMap() throws IOException {
|
||||
public void testJsonToMap() {
|
||||
Map<String, Object> map = Json.toMap("{\"map\":{\"a\":\"b\"}}");
|
||||
assertTrue(map.get("map") instanceof Map);
|
||||
assertEquals("b", ((Map<String, Object>) map.get("map")).get("a"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMapInList() throws IOException {
|
||||
JsonBuilder builder = new JsonBuilder();
|
||||
String field = "name";
|
||||
String value = "Jörg";
|
||||
builder.beginMap().
|
||||
beginMap("bool")
|
||||
.beginCollection("should")
|
||||
.beginMap()
|
||||
.beginMap("simple_query_string")
|
||||
.field("query", value)
|
||||
.field("fields", Collections.singletonList(field))
|
||||
.field("default_operator", "and")
|
||||
.endMap()
|
||||
.endMap()
|
||||
.beginMap()
|
||||
.beginMap("simple_query_string")
|
||||
.field("query", "\"" + value + "\"")
|
||||
.field("fields", Collections.singletonList(field + "^2"))
|
||||
.field("default_operator", "and")
|
||||
.endMap()
|
||||
.endMap()
|
||||
.endCollection()
|
||||
.field("minimum_should_match", "1")
|
||||
.endMap()
|
||||
.endMap();
|
||||
assertEquals("{\"bool\":{\"should\":[{\"simple_query_string\":{\"query\":\"Jörg\",\"fields\":[\"name\"],\"default_operator\":\"and\"}},{\"simple_query_string\":{\"query\":\"\\\"Jörg\\\"\",\"fields\":[\"name^2\"],\"default_operator\":\"and\"}}],\"minimum_should_match\":\"1\"}}", builder.build());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue