Search in sources :

Example 1 with JSONString

use of com.yahoo.prelude.hitfield.JSONString 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());
}
Also used : Value(com.yahoo.data.access.simple.Value) Inspector(com.yahoo.data.access.Inspector) JSONString(com.yahoo.prelude.hitfield.JSONString) JSONString(com.yahoo.prelude.hitfield.JSONString) Test(org.junit.Test)

Example 2 with JSONString

use of com.yahoo.prelude.hitfield.JSONString in project vespa by vespa-engine.

the class JSONStringTestCase method testArrayOfStructWithEmptyMap.

@Test
public void testArrayOfStructWithEmptyMap() {
    String json = "[{\"asf\":\"here is 1st simple string field\",\"map\":[],\"sf2\":\"here is 2nd simple string field\"},{\"asf\":\"here is 3rd simple string field\",\"map\":[],\"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\"></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\"></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()).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()).put("sf2", "here is 4th simple string field"));
    js = new JSONString(top);
    assertEquals(correct, js.renderFromInspector());
}
Also used : Value(com.yahoo.data.access.simple.Value) Inspector(com.yahoo.data.access.Inspector) JSONString(com.yahoo.prelude.hitfield.JSONString) JSONString(com.yahoo.prelude.hitfield.JSONString) Test(org.junit.Test)

Example 3 with JSONString

use of com.yahoo.prelude.hitfield.JSONString in project vespa by vespa-engine.

the class JSONStringTestCase method testContentToInspectorMapping.

@Test
public void testContentToInspectorMapping() {
    Inspector value1 = new JSONString("").inspect();
    Inspector value2 = new JSONString("foo").inspect();
    Inspector value3 = new JSONString("\"foo\"").inspect();
    Inspector value4 = new JSONString("123").inspect();
    Inspector value5 = new JSONString("{\"foo\":1}").inspect();
    Inspector value6 = new JSONString("[1,2,3]").inspect();
    System.out.println("1: " + value1);
    System.out.println("2: " + value2);
    System.out.println("3: " + value3);
    System.out.println("4: " + value4);
    System.out.println("5: " + value5);
    System.out.println("6: " + value6);
    assertEquals(Type.STRING, value1.type());
    assertEquals("", value1.asString());
    assertEquals(value2.type(), Type.STRING);
    assertEquals("foo", value2.asString());
    assertEquals(value3.type(), Type.STRING);
    assertEquals("\"foo\"", value3.asString());
    assertEquals(value4.type(), Type.STRING);
    assertEquals("123", value4.asString());
    assertEquals(value5.type(), Type.OBJECT);
    assertEquals(1L, value5.field("foo").asLong());
    assertEquals("{\"foo\":1}", value5.toString());
    assertEquals(value6.type(), Type.ARRAY);
    assertEquals(1L, value6.entry(0).asLong());
    assertEquals(2L, value6.entry(1).asLong());
    assertEquals(3L, value6.entry(2).asLong());
    assertEquals("[1,2,3]", value6.toString());
}
Also used : Inspector(com.yahoo.data.access.Inspector) JSONString(com.yahoo.prelude.hitfield.JSONString) Test(org.junit.Test)

Example 4 with JSONString

use of com.yahoo.prelude.hitfield.JSONString in project vespa by vespa-engine.

the class JSONStringTestCase method testWithData.

@Test
public void testWithData() {
    byte[] d1 = { (byte) 0x41, (byte) 0x42, (byte) 0x43 };
    byte[] d2 = { (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x02 };
    byte[] d3 = { (byte) 0x12, (byte) 0x34 };
    byte[] d4 = { (byte) 0xff, (byte) 0x80, (byte) 0x7f };
    Inspector top = new Value.ObjectValue().put("simple", new Value.DataValue(d1)).put("array", new Value.ArrayValue().add(new Value.DataValue(d2)).add(new Value.DataValue(d3)).add(new Value.DataValue(d4)));
    JSONString js = new JSONString(top);
    String correct = "\n" + "      <struct-field name=\"simple\">" + "<data length=\"3\" encoding=\"hex\">414243</data>" + "</struct-field>\n" + "      <struct-field name=\"array\">\n" + "        <item>" + "<data length=\"4\" encoding=\"hex\">00010002</data>" + "</item>\n" + "        <item>" + "<data length=\"2\" encoding=\"hex\">1234</data>" + "</item>\n" + "        <item>" + "<data length=\"3\" encoding=\"hex\">FF807F</data>" + "</item>\n" + "      </struct-field>\n    ";
    assertEquals(correct, js.renderFromInspector());
}
Also used : Value(com.yahoo.data.access.simple.Value) Inspector(com.yahoo.data.access.Inspector) JSONString(com.yahoo.prelude.hitfield.JSONString) JSONString(com.yahoo.prelude.hitfield.JSONString) Test(org.junit.Test)

Example 5 with JSONString

use of com.yahoo.prelude.hitfield.JSONString in project vespa by vespa-engine.

the class JSONStringTestCase method testInspectorToContentMapping.

@Test
public void testInspectorToContentMapping() {
    String content1 = new JSONString(getSlime1()).getContent();
    String content2 = new JSONString(getSlime2()).getContent();
    String content3 = new JSONString(getSlime3()).getContent();
    String content4 = new JSONString(getSlime4()).getContent();
    String content5 = new JSONString(getSlime5()).getContent();
    assertEquals("", content1);
    assertEquals("foo", content2);
    assertEquals("123", content3);
    assertEquals("{\"foo\":1}", content4);
    assertEquals("[1,2,3]", content5);
}
Also used : JSONString(com.yahoo.prelude.hitfield.JSONString) JSONString(com.yahoo.prelude.hitfield.JSONString) Test(org.junit.Test)

Aggregations

JSONString (com.yahoo.prelude.hitfield.JSONString)19 Test (org.junit.Test)15 Inspector (com.yahoo.data.access.Inspector)8 Value (com.yahoo.data.access.simple.Value)7 XMLString (com.yahoo.prelude.hitfield.XMLString)4 Result (com.yahoo.search.Result)4 FastHit (com.yahoo.prelude.fastsearch.FastHit)3 Hit (com.yahoo.search.result.Hit)3 StructuredData (com.yahoo.search.result.StructuredData)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 Chain (com.yahoo.component.chain.Chain)1 SlimeAdapter (com.yahoo.data.access.slime.SlimeAdapter)1 SlowInflate (com.yahoo.io.SlowInflate)1 RawData (com.yahoo.prelude.hitfield.RawData)1 Query (com.yahoo.search.Query)1 FeatureData (com.yahoo.search.result.FeatureData)1 Relevance (com.yahoo.search.result.Relevance)1 Execution (com.yahoo.search.searchchain.Execution)1