Search in sources :

Example 1 with Json

use of org.infinispan.commons.dataconversion.internal.Json in project infinispan by infinispan.

the class JsonConfigurationReader method processArray.

private void processArray(String name, Json value) {
    attributes.clear();
    List<Json> list = value.asJsonList();
    List<ElementEntry> array = new ArrayList<>(list.size() * 2 + 2);
    boolean primitive = (list.size() > 0 && list.get(0).isPrimitive());
    if (!primitive) {
        array.add(new ElementEntry(name, null, ElementType.START_ELEMENT));
    }
    for (Json json : list) {
        array.add(new ElementEntry(name, json, ElementType.START_ELEMENT));
    }
    if (!primitive) {
        array.add(new ElementEntry(name, null, ElementType.END_ELEMENT));
    }
    Iterator<ElementEntry> it = array.iterator();
    iteratorStack.push(it);
    // Already remove the first one
    element = it.next().v;
}
Also used : ArrayList(java.util.ArrayList) Json(org.infinispan.commons.dataconversion.internal.Json)

Example 2 with Json

use of org.infinispan.commons.dataconversion.internal.Json in project infinispan by infinispan.

the class JsonConfigurationWriter method writeAttribute.

@Override
public void writeAttribute(String name, boolean value) {
    Json parent = attributeParent();
    parent.set(name, value);
}
Also used : Json(org.infinispan.commons.dataconversion.internal.Json)

Example 3 with Json

use of org.infinispan.commons.dataconversion.internal.Json in project infinispan by infinispan.

the class ProtostreamTranscoderTest method assertJsonCorrect.

private void assertJsonCorrect(Object json) {
    String strJson = json instanceof byte[] ? new String((byte[]) json) : json.toString();
    Json jsonResult = Json.read(strJson);
    assertEquals("org.infinispan.test.core.Address", jsonResult.at("_type").asString());
}
Also used : Json(org.infinispan.commons.dataconversion.internal.Json)

Example 4 with Json

use of org.infinispan.commons.dataconversion.internal.Json in project infinispan by infinispan.

the class BaseJsonTest method testHotRodInteroperability.

@Test
public void testHotRodInteroperability() {
    remoteCache.clear();
    // Put object via Hot Rod
    remoteCache.put("BTC", new CryptoCurrency("Bitcoin", 1));
    remoteCache.put("ETH", new CryptoCurrency("Ethereum", 2));
    remoteCache.put("XRP", new CryptoCurrency("Ripple", 3));
    remoteCache.put("CAT", new CryptoCurrency("Catcoin", 618));
    assertEquals(remoteCache.get("CAT").getDescription(), "Catcoin");
    assertEquals(remoteCache.size(), 4);
    Query<CryptoCurrency> query = Search.getQueryFactory(remoteCache).create("FROM " + getEntityName() + " c where c.rank < 10");
    List<CryptoCurrency> highRankCoins = query.execute().list();
    assertEquals(highRankCoins.size(), 3);
    // Read as Json
    CryptoCurrency btc = readCurrencyViaJson("BTC");
    assertEquals(btc.getDescription(), "Bitcoin");
    assertEquals(btc.getRank(), Integer.valueOf(1));
    // Write as Json
    writeCurrencyViaJson("LTC", "Litecoin", 4);
    // Assert inserted entity is searchable
    query = Search.getQueryFactory(remoteCache).create("FROM " + getEntityName() + " c  where c.description = 'Litecoin'");
    CryptoCurrency litecoin = query.execute().list().iterator().next();
    assertEquals(litecoin.getDescription(), "Litecoin");
    assertEquals(litecoin.getRank(), Integer.valueOf(4));
    // Read as JSON from the Hot Rod client
    Object jsonResult = remoteCache.withDataFormat(DataFormat.builder().valueType(MediaType.APPLICATION_JSON).build()).get("LTC");
    Json jsonNode = Json.read(new String((byte[]) jsonResult, StandardCharsets.UTF_8));
    assertEquals(jsonNode.at("description").asString(), "Litecoin");
}
Also used : Json(org.infinispan.commons.dataconversion.internal.Json) Test(org.testng.annotations.Test) AbstractInfinispanTest(org.infinispan.test.AbstractInfinispanTest)

Example 5 with Json

use of org.infinispan.commons.dataconversion.internal.Json in project infinispan by infinispan.

the class ComplexValue method testBatchOperations.

@Test
public void testBatchOperations() {
    remoteCache.clear();
    Map<ComplexKey, ComplexValue> entries = new HashMap<>();
    IntStream.range(0, 50).forEach(i -> {
        ComplexKey key = new ComplexKey(String.valueOf(i), (float) i);
        ComplexValue value = new ComplexValue(Util.threadLocalRandomUUID());
        entries.put(key, value);
    });
    remoteCache.putAll(entries);
    // Read all keys as JSON Strings
    RemoteCache<String, String> jsonCache = this.remoteCache.withDataFormat(DataFormat.builder().keyType(APPLICATION_JSON).keyMarshaller(new UTF8StringMarshaller()).build());
    Set<String> jsonKeys = new HashSet<>(jsonCache.keySet());
    jsonKeys.forEach(k -> assertTrue(k.contains("\"_type\": \"org.infinispan.test.client.DataFormatTest.ComplexKey\"")));
    Map<String, String> newEntries = new HashMap<>();
    // Write using JSON
    IntStream.range(50, 100).forEach(i -> {
        Json key = Json.object("_type", "org.infinispan.test.client.DataFormatTest.ComplexKey").set("id", i).set("ratio", i);
        Json value = Json.object("_type", "org.infinispan.test.client.DataFormatTest.ComplexValue").set("uuid", Util.threadLocalRandomUUID().toString());
        newEntries.put(key.toString(), value.toString());
    });
    jsonCache.putAll(newEntries);
    // Read it back as regular objects
    Set<ComplexKey> keys = new HashSet<>();
    IntStream.range(60, 70).forEach(i -> keys.add(new ComplexKey(String.valueOf(i), (float) i)));
    Set<ComplexKey> returned = remoteCache.getAll(keys).keySet().stream().map(ComplexKey.class::cast).collect(Collectors.toSet());
    assertEquals(keys, returned);
}
Also used : HashMap(java.util.HashMap) UTF8StringMarshaller(org.infinispan.commons.marshall.UTF8StringMarshaller) Json(org.infinispan.commons.dataconversion.internal.Json) HashSet(java.util.HashSet) Test(org.testng.annotations.Test) SingleHotRodServerTest(org.infinispan.client.hotrod.test.SingleHotRodServerTest)

Aggregations

Json (org.infinispan.commons.dataconversion.internal.Json)130 RestResponse (org.infinispan.client.rest.RestResponse)51 Test (org.testng.annotations.Test)51 RestClient (org.infinispan.client.rest.RestClient)15 Util.getResourceAsString (org.infinispan.commons.util.Util.getResourceAsString)13 Test (org.junit.Test)13 RestCacheClient (org.infinispan.client.rest.RestCacheClient)12 MultipleCacheManagersTest (org.infinispan.test.MultipleCacheManagersTest)12 AbstractMultipleSitesTest (org.infinispan.xsite.AbstractMultipleSitesTest)7 ArrayList (java.util.ArrayList)6 List (java.util.List)6 Map (java.util.Map)6 NettyRestResponse (org.infinispan.rest.NettyRestResponse)6 ResourceUtil.addEntityAsJson (org.infinispan.rest.resources.ResourceUtil.addEntityAsJson)6 HashMap (java.util.HashMap)5 RestEntity (org.infinispan.client.rest.RestEntity)5 IOException (java.io.IOException)4 HashSet (java.util.HashSet)4 Request (okhttp3.Request)4 RestSchemaClient (org.infinispan.client.rest.RestSchemaClient)4