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