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
|
@Override
|
||||||
public Builder beginMap() throws IOException {
|
public Builder beginMap() throws IOException {
|
||||||
|
if (state.structure == Structure.COLLECTION) {
|
||||||
|
beginArrayValue();
|
||||||
|
}
|
||||||
this.state = new State(state, state.level + 1, Structure.MAP, true);
|
this.state = new State(state, state.level + 1, Structure.MAP, true);
|
||||||
appendable.append('{');
|
appendable.append('{');
|
||||||
return this;
|
return this;
|
||||||
|
@ -70,9 +73,6 @@ public class JsonBuilder implements Builder {
|
||||||
@Override
|
@Override
|
||||||
public Builder buildMap(Map<String, Object> map) throws IOException {
|
public Builder buildMap(Map<String, Object> map) throws IOException {
|
||||||
Objects.requireNonNull(map);
|
Objects.requireNonNull(map);
|
||||||
if (state.structure == Structure.COLLECTION) {
|
|
||||||
beginArrayValue(map);
|
|
||||||
}
|
|
||||||
boolean wrap = state.structure != Structure.MAP;
|
boolean wrap = state.structure != Structure.MAP;
|
||||||
if (wrap) {
|
if (wrap) {
|
||||||
beginMap();
|
beginMap();
|
||||||
|
@ -88,9 +88,6 @@ public class JsonBuilder implements Builder {
|
||||||
if (wrap) {
|
if (wrap) {
|
||||||
endMap();
|
endMap();
|
||||||
}
|
}
|
||||||
if (state.structure == Structure.COLLECTION) {
|
|
||||||
endArrayValue(map);
|
|
||||||
}
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -119,10 +116,8 @@ public class JsonBuilder implements Builder {
|
||||||
buildCollection((Collection<Object>) object);
|
buildCollection((Collection<Object>) object);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
if (state.structure == Structure.MAP || state.structure == Structure.KEY) {
|
if (state.structure == Structure.COLLECTION) {
|
||||||
beginValue(object);
|
beginArrayValue();
|
||||||
} else if (state.structure == Structure.COLLECTION) {
|
|
||||||
beginArrayValue(object);
|
|
||||||
}
|
}
|
||||||
if (object == null) {
|
if (object == null) {
|
||||||
buildNull();
|
buildNull();
|
||||||
|
@ -151,18 +146,13 @@ public class JsonBuilder implements Builder {
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException("unable to write object class " + object.getClass());
|
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;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Builder buildKey(CharSequence string) throws IOException {
|
public Builder buildKey(CharSequence string) throws IOException {
|
||||||
if (state.structure == Structure.COLLECTION) {
|
if (state.structure == Structure.COLLECTION) {
|
||||||
beginArrayValue(string);
|
beginArrayValue();
|
||||||
} else if (state.structure == Structure.MAP || state.structure == Structure.KEY) {
|
} else if (state.structure == Structure.MAP || state.structure == Structure.KEY) {
|
||||||
beginKey(string != null ? string.toString() : null);
|
beginKey(string != null ? string.toString() : null);
|
||||||
}
|
}
|
||||||
|
@ -176,34 +166,19 @@ public class JsonBuilder implements Builder {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Builder buildNull() throws IOException {
|
public Builder buildNull() throws IOException {
|
||||||
if (state.structure == Structure.MAP || state.structure == Structure.KEY) {
|
if (state.structure == Structure.COLLECTION) {
|
||||||
beginValue(null);
|
beginArrayValue();
|
||||||
} else if (state.structure == Structure.COLLECTION) {
|
|
||||||
beginArrayValue(null);
|
|
||||||
}
|
}
|
||||||
buildString("null", false);
|
buildString("null", false);
|
||||||
if (state.structure == Structure.MAP || state.structure == Structure.KEY) {
|
|
||||||
endValue(null);
|
|
||||||
} else if (state.structure == Structure.COLLECTION) {
|
|
||||||
endArrayValue(null);
|
|
||||||
}
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Builder copy(Builder builder) throws IOException {
|
public Builder copy(Builder builder) throws IOException {
|
||||||
if (state.structure == Structure.MAP || state.structure == Structure.KEY) {
|
if (state.structure == Structure.COLLECTION) {
|
||||||
beginValue(null);
|
beginArrayValue();
|
||||||
} else if (state.structure == Structure.COLLECTION) {
|
|
||||||
beginArrayValue(null);
|
|
||||||
}
|
}
|
||||||
appendable.append(builder.build());
|
appendable.append(builder.build());
|
||||||
if (state.structure == Structure.MAP || state.structure == Structure.KEY) {
|
|
||||||
endValue(null);
|
|
||||||
}
|
|
||||||
if (state.structure == Structure.COLLECTION) {
|
|
||||||
endArrayValue(null);
|
|
||||||
}
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -224,13 +199,7 @@ public class JsonBuilder implements Builder {
|
||||||
appendable.append(":");
|
appendable.append(":");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void beginValue(Object v) {
|
private void beginArrayValue() throws IOException {
|
||||||
}
|
|
||||||
|
|
||||||
private void endValue(Object v) {
|
|
||||||
}
|
|
||||||
|
|
||||||
private void beginArrayValue(Object v) throws IOException {
|
|
||||||
if (state.first) {
|
if (state.first) {
|
||||||
state.first = false;
|
state.first = false;
|
||||||
} else {
|
} else {
|
||||||
|
@ -238,9 +207,6 @@ public class JsonBuilder implements Builder {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void endArrayValue(Object v) {
|
|
||||||
}
|
|
||||||
|
|
||||||
private void buildBoolean(boolean bool) throws IOException {
|
private void buildBoolean(boolean bool) throws IOException {
|
||||||
buildString(bool ? "true" : "false", false);
|
buildString(bool ? "true" : "false", false);
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,6 +8,7 @@ import org.xbib.datastructures.json.tiny.StreamParser;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.StringReader;
|
import java.io.StringReader;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
@ -233,9 +234,38 @@ public class JsonBuilderTest {
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
@Test
|
@Test
|
||||||
public void testJsonToMap() throws IOException {
|
public void testJsonToMap() {
|
||||||
Map<String, Object> map = Json.toMap("{\"map\":{\"a\":\"b\"}}");
|
Map<String, Object> map = Json.toMap("{\"map\":{\"a\":\"b\"}}");
|
||||||
assertTrue(map.get("map") instanceof Map);
|
assertTrue(map.get("map") instanceof Map);
|
||||||
assertEquals("b", ((Map<String, Object>) map.get("map")).get("a"));
|
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