Search in sources :

Example 26 with JSONParser

use of org.noggit.JSONParser 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 27 with JSONParser

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

the class CollectionTester method match.

/**
   * @param path JSON path expression
   * @param input JSON Structure to parse and test against
   * @param expected expected value of path
   * @param delta tollerance allowed in comparing float/double values
   */
public static String match(String path, String input, String expected, double delta) throws Exception {
    Object inputObj = failRepeatedKeys ? new NoDupsObjectBuilder(new JSONParser(input)).getVal() : ObjectBuilder.fromJSON(input);
    Object expectObj = failRepeatedKeys ? new NoDupsObjectBuilder(new JSONParser(expected)).getVal() : ObjectBuilder.fromJSON(expected);
    return matchObj(path, inputObj, expectObj, delta);
}
Also used : JSONParser(org.noggit.JSONParser)

Example 28 with JSONParser

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

the class CollectionTester method matchObj.

/**
   * @param input Object structure to parse and test against
   * @param pathAndExpected JSON path expression + '==' + expected value
   * @param delta tollerance allowed in comparing float/double values
   */
public static String matchObj(Object input, String pathAndExpected, double delta) throws Exception {
    int pos = pathAndExpected.indexOf("==");
    String path = pos >= 0 ? pathAndExpected.substring(0, pos) : null;
    String expected = pos >= 0 ? pathAndExpected.substring(pos + 2) : pathAndExpected;
    Object expectObj = failRepeatedKeys ? new NoDupsObjectBuilder(new JSONParser(expected)).getVal() : ObjectBuilder.fromJSON(expected);
    return matchObj(path, input, expectObj, delta);
}
Also used : JSONParser(org.noggit.JSONParser)

Example 29 with JSONParser

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

the class SolrCloudExampleTest method getAsMap.

private Map getAsMap(CloudSolrClient cloudClient, String uri) throws Exception {
    HttpGet get = new HttpGet(uri);
    HttpEntity entity = null;
    try {
        entity = cloudClient.getLbClient().getHttpClient().execute(get).getEntity();
        String response = EntityUtils.toString(entity, StandardCharsets.UTF_8);
        return (Map) ObjectBuilder.getVal(new JSONParser(new StringReader(response)));
    } finally {
        EntityUtils.consumeQuietly(entity);
    }
}
Also used : HttpEntity(org.apache.http.HttpEntity) HttpGet(org.apache.http.client.methods.HttpGet) StringReader(java.io.StringReader) JSONParser(org.noggit.JSONParser) HashMap(java.util.HashMap) Map(java.util.Map)

Example 30 with JSONParser

use of org.noggit.JSONParser 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)37 Map (java.util.Map)30 StringReader (java.io.StringReader)25 RestTestHarness (org.apache.solr.util.RestTestHarness)12 ArrayList (java.util.ArrayList)10 List (java.util.List)10 IOException (java.io.IOException)6 HashSet (java.util.HashSet)6 LinkedHashMap (java.util.LinkedHashMap)6 HttpEntity (org.apache.http.HttpEntity)6 Test (org.junit.Test)5 HashMap (java.util.HashMap)4 SolrException (org.apache.solr.common.SolrException)4 HttpGet (org.apache.http.client.methods.HttpGet)3 HttpPost (org.apache.http.client.methods.HttpPost)3 ByteArrayEntity (org.apache.http.entity.ByteArrayEntity)3 NamedList (org.apache.solr.common.util.NamedList)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2