Search in sources :

Example 1 with ObjectBuilder

use of org.noggit.ObjectBuilder in project lucene-solr by apache.

the class SmileWriterTest method testJSON.

@Test
public void testJSON() throws IOException {
    SolrQueryRequest req = req("wt", "json", "json.nl", "arrarr");
    SolrQueryResponse rsp = new SolrQueryResponse();
    SmileResponseWriter w = new SmileResponseWriter();
    ByteArrayOutputStream buf = new ByteArrayOutputStream();
    NamedList nl = new NamedList();
    // make sure that 2028 and 2029 are both escaped (they are illegal in javascript)
    nl.add("data1", "he
llo
!");
    nl.add(null, 42);
    rsp.add("nl", nl);
    rsp.add("byte", Byte.valueOf((byte) -3));
    rsp.add("short", Short.valueOf((short) -4));
    String expected = "{\"nl\":[[\"data1\",\"he\\u2028llo\\u2029!\"],[null,42]],byte:-3,short:-4}";
    w.write(buf, req, rsp);
    Map m = (Map) decodeSmile(new ByteArrayInputStream(buf.toByteArray()));
    Map o2 = (Map) new ObjectBuilder(new JSONParser(new StringReader(expected))).getObject();
    assertEquals(Utils.toJSONString(m), Utils.toJSONString(o2));
    req.close();
}
Also used : SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) LocalSolrQueryRequest(org.apache.solr.request.LocalSolrQueryRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) NamedList(org.apache.solr.common.util.NamedList) StringReader(java.io.StringReader) JSONParser(org.noggit.JSONParser) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectBuilder(org.noggit.ObjectBuilder) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Test(org.junit.Test)

Example 2 with ObjectBuilder

use of org.noggit.ObjectBuilder in project lucene-solr by apache.

the class TestJsonFacetRefinement method fromJSON.

/** Use SimpleOrderedMap rather than Map to match responses from shards */
public static Object fromJSON(String json) throws IOException {
    JSONParser parser = new JSONParser(json);
    ObjectBuilder ob = new ObjectBuilder(parser) {

        @Override
        public Object newObject() throws IOException {
            return new SimpleOrderedMap();
        }

        @Override
        public void addKeyVal(Object map, Object key, Object val) throws IOException {
            ((SimpleOrderedMap) map).add(key.toString(), val);
        }
    };
    return ob.getObject();
}
Also used : JSONParser(org.noggit.JSONParser) ObjectBuilder(org.noggit.ObjectBuilder) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap)

Example 3 with ObjectBuilder

use of org.noggit.ObjectBuilder in project lucene-solr by apache.

the class CommandOperation method parse.

/**
   * Parse the command operations into command objects
   */
public static List<CommandOperation> parse(Reader rdr) throws IOException {
    JSONParser parser = new JSONParser(rdr);
    parser.setFlags(parser.getFlags() | JSONParser.ALLOW_MISSING_COLON_COMMA_BEFORE_OBJECT | JSONParser.OPTIONAL_OUTER_BRACES);
    ObjectBuilder ob = new ObjectBuilder(parser);
    if (parser.lastEvent() != JSONParser.OBJECT_START) {
        throw new RuntimeException("The JSON must be an Object of the form {\"command\": {...},...");
    }
    List<CommandOperation> operations = new ArrayList<>();
    for (; ; ) {
        int ev = parser.nextEvent();
        if (ev == JSONParser.OBJECT_END)
            return operations;
        Object key = ob.getKey();
        ev = parser.nextEvent();
        Object val = ob.getVal();
        if (val instanceof List) {
            List list = (List) val;
            for (Object o : list) {
                if (!(o instanceof Map)) {
                    operations.add(new CommandOperation(String.valueOf(key), list));
                    break;
                } else {
                    operations.add(new CommandOperation(String.valueOf(key), o));
                }
            }
        } else {
            operations.add(new CommandOperation(String.valueOf(key), val));
        }
    }
}
Also used : ArrayList(java.util.ArrayList) JSONParser(org.noggit.JSONParser) ArrayList(java.util.ArrayList) Collections.singletonList(java.util.Collections.singletonList) List(java.util.List) ObjectBuilder(org.noggit.ObjectBuilder) Collections.emptyMap(java.util.Collections.emptyMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Collections.singletonMap(java.util.Collections.singletonMap)

Aggregations

JSONParser (org.noggit.JSONParser)3 ObjectBuilder (org.noggit.ObjectBuilder)3 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 StringReader (java.io.StringReader)1 ArrayList (java.util.ArrayList)1 Collections.emptyMap (java.util.Collections.emptyMap)1 Collections.singletonList (java.util.Collections.singletonList)1 Collections.singletonMap (java.util.Collections.singletonMap)1 List (java.util.List)1 NamedList (org.apache.solr.common.util.NamedList)1 SimpleOrderedMap (org.apache.solr.common.util.SimpleOrderedMap)1 LocalSolrQueryRequest (org.apache.solr.request.LocalSolrQueryRequest)1 SolrQueryRequest (org.apache.solr.request.SolrQueryRequest)1 Test (org.junit.Test)1