Search in sources :

Example 21 with JSONParser

use of org.noggit.JSONParser in project disgear by yangbutao.

the class ZkStateReader method fromJSON.

public static Object fromJSON(byte[] utf8) {
    // convert directly from bytes to chars
    // and parse directly from that instead of going through
    // intermediate strings or readers
    CharArr chars = new CharArr();
    ByteUtils.UTF8toUTF16(utf8, 0, utf8.length, chars);
    JSONParser parser = new JSONParser(chars.getArray(), chars.getStart(), chars.length());
    try {
        return ObjectBuilder.getVal(parser);
    } catch (IOException e) {
        // should never happen w/o using real
        throw new RuntimeException(e);
    // IO
    }
}
Also used : JSONParser(org.noggit.JSONParser) IOException(java.io.IOException) CharArr(org.noggit.CharArr)

Example 22 with JSONParser

use of org.noggit.JSONParser in project ranger by apache.

the class SolrCollectionBootstrapper method postDataAndGetResponse.

public static Map postDataAndGetResponse(CloudSolrClient cloudClient, String uri, ByteBuffer bytarr) throws IOException {
    HttpPost httpPost = null;
    HttpEntity entity;
    String response = null;
    Map m = null;
    try {
        httpPost = new HttpPost(uri);
        httpPost.setHeader("Content-Type", "application/octet-stream");
        httpPost.setEntity(new ByteArrayEntity(bytarr.array(), bytarr.arrayOffset(), bytarr.limit()));
        entity = cloudClient.getLbClient().getHttpClient().execute(httpPost).getEntity();
        try {
            response = EntityUtils.toString(entity, StandardCharsets.UTF_8);
            m = (Map) ObjectBuilder.getVal(new JSONParser(new StringReader(response)));
        } catch (JSONParser.ParseException e) {
            logger.severe("Error response: " + response);
        }
    } finally {
        httpPost.releaseConnection();
    }
    return m;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpEntity(org.apache.http.HttpEntity) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) StringReader(java.io.StringReader) JSONParser(org.noggit.JSONParser) Map(java.util.Map)

Example 23 with JSONParser

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

the class TestSolrConfigHandler method runConfigCommand.

public static void runConfigCommand(RestTestHarness harness, String uri, String payload) throws IOException {
    String json = SolrTestCaseJ4.json(payload);
    log.info("going to send config command. path {} , payload: {}", uri, payload);
    String response = harness.post(uri, json);
    Map map = (Map) ObjectBuilder.getVal(new JSONParser(new StringReader(response)));
    assertNull(response, map.get("errors"));
}
Also used : StringReader(java.io.StringReader) JSONParser(org.noggit.JSONParser) TestBlobHandler.getAsString(org.apache.solr.handler.TestBlobHandler.getAsString) HashMap(java.util.HashMap) Map(java.util.Map) ValidatingJsonMap(org.apache.solr.common.util.ValidatingJsonMap) TreeMap(java.util.TreeMap) SortedMap(java.util.SortedMap)

Example 24 with JSONParser

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

the class TestConfigSetImmutable method testSchemaHandlerImmutable.

@Test
public void testSchemaHandlerImmutable() throws Exception {
    String payload = "{\n" + "    'add-field' : {\n" + "                 'name':'a1',\n" + "                 'type': 'string',\n" + "                 'stored':true,\n" + "                 'indexed':false\n" + "                 },\n" + "    }";
    String response = restTestHarness.post("/schema?wt=json", json(payload));
    Map map = (Map) ObjectBuilder.getVal(new JSONParser(new StringReader(response)));
    assertNotNull(map.get("errors"));
    assertTrue(map.get("errors").toString().contains("immutable"));
}
Also used : StringReader(java.io.StringReader) JSONParser(org.noggit.JSONParser) Map(java.util.Map) Test(org.junit.Test)

Example 25 with JSONParser

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

the class TestManagedResource method testLoadingAndStoringOfManagedData.

/**
   * Tests managed data storage to and loading from {@link ManagedResourceStorage.InMemoryStorageIO}.
   */
@SuppressWarnings("unchecked")
@Test
public void testLoadingAndStoringOfManagedData() throws Exception {
    String resourceId = "/config/test/foo";
    String storedResourceId = "_config_test_foo.json";
    MockAnalysisComponent observer = new MockAnalysisComponent();
    List<ManagedResourceObserver> observers = Arrays.asList((ManagedResourceObserver) observer);
    // put some data in the storage impl so that we can test 
    // initialization of managed data from storage
    String storedJson = "{'initArgs':{'someArg':'someVal', 'arg2':true, 'arg3':['one','two','three']," + " 'arg4':18, 'arg5':0.9, 'arg6':{ 'uno':1, 'dos':2}},'" + ManagedResource.MANAGED_JSON_LIST_FIELD + "':['1','2','3']}";
    ManagedResourceStorage.InMemoryStorageIO storageIO = new ManagedResourceStorage.InMemoryStorageIO();
    storageIO.storage.put(storedResourceId, new BytesRef(json(storedJson)));
    ManagedTestResource res = new ManagedTestResource(resourceId, new SolrResourceLoader(Paths.get("./")), storageIO);
    res.loadManagedDataAndNotify(observers);
    assertTrue("Observer was not notified by ManagedResource!", observer.wasNotified);
    // now update the managed data (as if it came from the REST API)
    List<String> updatedData = new ArrayList<>();
    updatedData.add("1");
    updatedData.add("2");
    updatedData.add("3");
    updatedData.add("4");
    res.storeManagedData(updatedData);
    StringReader stringReader = new StringReader(storageIO.storage.get(storedResourceId).utf8ToString());
    Map<String, Object> jsonObject = (Map<String, Object>) ObjectBuilder.getVal(new JSONParser(stringReader));
    List<String> jsonList = (List<String>) jsonObject.get(ManagedResource.MANAGED_JSON_LIST_FIELD);
    assertTrue("Managed data was not updated correctly!", jsonList.contains("4"));
}
Also used : ArrayList(java.util.ArrayList) SolrResourceLoader(org.apache.solr.core.SolrResourceLoader) StringReader(java.io.StringReader) JSONParser(org.noggit.JSONParser) NamedList(org.apache.solr.common.util.NamedList) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) BytesRef(org.apache.lucene.util.BytesRef) Test(org.junit.Test)

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