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();
}
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();
}
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));
}
}
}
Aggregations