use of com.yahoo.data.access.simple.Value in project vespa by vespa-engine.
the class JSONStringTestCase method testMap.
@Test
public void testMap() {
String json = "[{\"key\":\"k1\",\"value\":\"v1\"},{\"key\":\"k2\",\"value\":\"v2\"}]";
JSONString js = new JSONString(json);
String correct = "\n" + " <item><key>k1</key><value>v1</value></item>\n" + " <item><key>k2</key><value>v2</value></item>\n ";
assertEquals(correct, js.toString());
Inspector top = new Value.ArrayValue().add(new Value.ObjectValue().put("key", "k1").put("value", "v1")).add(new Value.ObjectValue().put("key", "k2").put("value", "v2"));
js = new JSONString(top);
assertEquals(correct, js.renderFromInspector());
}
use of com.yahoo.data.access.simple.Value in project vespa by vespa-engine.
the class JSONString method inspect.
public Inspector inspect() {
if (value == null) {
JsonDecoder decoder = new JsonDecoder();
Slime slime = decoder.decode(new Slime(), Utf8.toBytes(content));
if (slime.get().field("error_message").valid() && slime.get().field("partial_result").valid() && slime.get().field("offending_input").valid()) {
// probably a json parse error...
value = new Value.StringValue(content);
} else if (slime.get().type() == com.yahoo.slime.Type.OBJECT || slime.get().type() == com.yahoo.slime.Type.ARRAY) {
// valid json object or array
value = new SlimeAdapter(slime.get());
} else {
// 'valid' json, but leaf value
value = new Value.StringValue(content);
}
}
return value;
}
use of com.yahoo.data.access.simple.Value in project vespa by vespa-engine.
the class JSONStringTestCase method testArrayOfStructWithMap.
/**
* here is some json for you
*
* [{"asf":"here is 1st simple string field",
* "map":[{"key":"one key string","value":["one value string","embedded array"]},
* {"key":"two key string","value":["two value string","embedded array"]}],
* "sf2":"here is 2nd simple string field"},
* {"asf":"here is 3rd simple string field",
* "map":[{"key":"three key string","value":["three value string","embedded array"]},
* {"key":"four key string","value":["four value string","embedded array"]}],
* "sf2":"here is 4th simple string field"},
* ]
*
**
*/
/**
* and here is some corresponding XML
*
* <item>
* <struct-field name="asf">here is 1st simple string field</struct-field>
* <struct-field name="map">
* <item><key>one key string</key><value>
* <item>one value string</item>
* <item>embedded array</item>
* </value></item>
* <item><key>two key string</key><value>
* <item>two value string</item>
* <item>embedded array</item>
* </value></item>
* </struct-field>
* <struct-field name="sf2">here is 2nd simple string field</struct-field>
* </item>
* <item>
* <struct-field name="asf">here is 3rd simple string field</struct-field>
* <struct-field name="map">
* <item><key>three key string</key><value>
* <item>three value string</item>
* <item>embedded array</item>
* </value></item>
* <item><key>four key string</key><value>
* <item>four value string</item>
* <item>embedded array</item>
* </value></item>
* </struct-field>
* <struct-field name="sf2">here is 4th simple string field</struct-field>
* </item>
*
**
*/
@Test
public void testArrayOfStructWithMap() {
String json = "[{\"asf\":\"here is 1st simple string field\",\"map\":[{\"key\":\"one key string\",\"value\":[\"one value string\",\"embedded array\"]},{\"key\":\"two key string\",\"value\":[\"two value string\",\"embedded array\"]}],\"sf2\":\"here is 2nd simple string field\"},{\"asf\":\"here is 3rd simple string field\",\"map\":[{\"key\":\"three key string\",\"value\":[\"three value string\",\"embedded array\"]},{\"key\":\"four key string\",\"value\":[\"four value string\",\"embedded array\"]}],\"sf2\":\"here is 4th simple string field\"}]";
JSONString js = new JSONString(json);
String correct = "\n" + " <item>\n" + " <struct-field name=\"asf\">here is 1st simple string field</struct-field>\n" + " <struct-field name=\"map\">\n" + " <item><key>one key string</key><value>\n" + " <item>one value string</item>\n" + " <item>embedded array</item>\n" + " </value></item>\n" + " <item><key>two key string</key><value>\n" + " <item>two value string</item>\n" + " <item>embedded array</item>\n" + " </value></item>\n" + " </struct-field>\n" + " <struct-field name=\"sf2\">here is 2nd simple string field</struct-field>\n" + " </item>\n" + " <item>\n" + " <struct-field name=\"asf\">here is 3rd simple string field</struct-field>\n" + " <struct-field name=\"map\">\n" + " <item><key>three key string</key><value>\n" + " <item>three value string</item>\n" + " <item>embedded array</item>\n" + " </value></item>\n" + " <item><key>four key string</key><value>\n" + " <item>four value string</item>\n" + " <item>embedded array</item>\n" + " </value></item>\n" + " </struct-field>\n" + " <struct-field name=\"sf2\">here is 4th simple string field</struct-field>\n" + " </item>\n" + " ";
assertEquals(correct, js.toString());
Inspector top = new Value.ArrayValue().add(new Value.ObjectValue().put("asf", "here is 1st simple string field").put("map", new Value.ArrayValue().add(new Value.ObjectValue().put("key", "one key string").put("value", new Value.ArrayValue().add("one value string").add("embedded array"))).add(new Value.ObjectValue().put("key", "two key string").put("value", new Value.ArrayValue().add("two value string").add("embedded array")))).put("sf2", "here is 2nd simple string field")).add(new Value.ObjectValue().put("asf", "here is 3rd simple string field").put("map", new Value.ArrayValue().add(new Value.ObjectValue().put("key", "three key string").put("value", new Value.ArrayValue().add("three value string").add("embedded array"))).add(new Value.ObjectValue().put("key", "four key string").put("value", new Value.ArrayValue().add("four value string").add("embedded array")))).put("sf2", "here is 4th simple string field"));
js = new JSONString(top);
assertEquals(correct, js.renderFromInspector());
}
Aggregations