use of org.infinispan.commons.dataconversion.internal.Json in project infinispan by infinispan.
the class ComplexValue method testValueInMultipleFormats.
@Test
public void testValueInMultipleFormats() throws Exception {
remoteCache.clear();
String quote = "I find your lack of faith disturbing";
byte[] protostreamMarshalledQuote = marshall(quote);
// Write to the cache using the default marshaller
remoteCache.put(1, quote);
// Read it back as raw bytes using the same key
Object asBinary = remoteCache.withDataFormat(DataFormat.builder().valueMarshaller(IdentityMarshaller.INSTANCE).build()).get(1);
assertArrayEquals(protostreamMarshalledQuote, ((byte[]) asBinary));
// Read it back as UTF-8 byte[] using the same key
Object asUTF8 = remoteCache.withDataFormat(DataFormat.builder().valueType(TEXT_PLAIN).valueMarshaller(IdentityMarshaller.INSTANCE).build()).get(1);
assertArrayEquals(quote.getBytes(UTF_8), (byte[]) asUTF8);
// Read it back as String using the default marshaller for text/plain
Object asString = remoteCache.withDataFormat(DataFormat.builder().valueType(TEXT_PLAIN).build()).get(1);
assertEquals(quote, asString);
// Same, but with metadata
MetadataValue<Object> metadataValue = remoteCache.withDataFormat(DataFormat.builder().valueType(TEXT_PLAIN).build()).getWithMetadata(1);
assertEquals(quote, metadataValue.getValue());
// Get all entries avoiding de-serialization
Map<Object, Object> allEntries = remoteCache.withDataFormat(DataFormat.builder().valueMarshaller(IdentityMarshaller.INSTANCE).build()).getAll(new HashSet<>(Collections.singletonList(1)));
assertArrayEquals(protostreamMarshalledQuote, (byte[]) allEntries.get(1));
// Read value as JSON in the byte[] form, using the same key
Json expectedJson = Json.object("_type", "string").set("_value", quote);
Object asJSon = remoteCache.withDataFormat(DataFormat.builder().valueType(APPLICATION_JSON).build()).get(1);
assertEquals(expectedJson, Json.read(new String((byte[]) asJSon)));
Json asJsonNode = (Json) remoteCache.withDataFormat(DataFormat.builder().valueType(APPLICATION_JSON).valueMarshaller(new JsonMarshaller()).build()).get(1);
assertEquals(expectedJson, asJsonNode);
// Iterate values without unmarshalling
Object raw = remoteCache.withDataFormat(DataFormat.builder().valueType(APPLICATION_PROTOSTREAM).valueMarshaller(IdentityMarshaller.INSTANCE).build()).values().iterator().next();
assertArrayEquals(protostreamMarshalledQuote, ((byte[]) raw));
// Iterate values converting to JsonNode objects
asJsonNode = (Json) remoteCache.withDataFormat(DataFormat.builder().valueType(APPLICATION_JSON).valueMarshaller(new JsonMarshaller()).build()).values().iterator().next();
assertEquals(expectedJson, asJsonNode);
}
use of org.infinispan.commons.dataconversion.internal.Json in project infinispan by infinispan.
the class RestCacheManagerClientOkHttp method restore.
@Override
public CompletionStage<RestResponse> restore(String name, File backup, Map<String, List<String>> resources) {
Json json = resources != null ? Json.factory().make(resources) : Json.object();
RequestBody zipBody = new FileRestEntityOkHttp(MediaType.APPLICATION_ZIP, backup).toRequestBody();
RequestBody multipartBody = new MultipartBody.Builder().addFormDataPart("resources", json.toString()).addFormDataPart("backup", backup.getName(), zipBody).setType(MultipartBody.FORM).build();
Request.Builder builder = restore(name).post(multipartBody);
return client.execute(builder);
}
use of org.infinispan.commons.dataconversion.internal.Json in project infinispan by infinispan.
the class BaseJsonTest method writeCurrencyViaJson.
private void writeCurrencyViaJson(String key, String description, int rank) {
Json currency = Json.object();
currency.set(TYPE, getJsonType());
currency.set("description", description);
currency.set("rank", rank);
RestEntity value = RestEntity.create(MediaType.APPLICATION_JSON, currency.toString());
RestResponse response = join(restCacheClient.put(key, value));
assertEquals(response.getStatus(), 204);
}
use of org.infinispan.commons.dataconversion.internal.Json in project infinispan by infinispan.
the class BaseJsonTest method readCurrencyViaJson.
private CryptoCurrency readCurrencyViaJson(String key) {
RestResponse response = join(restCacheClient.get(key, MediaType.APPLICATION_JSON_TYPE));
String json = response.getBody();
Json jsonNode = Json.read(json);
Json description = jsonNode.at("description");
Json rank = jsonNode.at("rank");
return new CryptoCurrency(description.asString(), rank.asInteger());
}
use of org.infinispan.commons.dataconversion.internal.Json in project infinispan by infinispan.
the class PatchInfo method fromJson.
public static PatchInfo fromJson(Json json) {
Json brandName = json.at(BRAND_NAME);
Json sourceVersion = json.at(SOURCE_VERSION);
Json targetVersion = json.at(TARGET_VERSION);
Json qualifier = json.at(QUALIFIER);
Json creationDate = json.at(CREATION_DATE);
Json installationDate = json.at(INSTALLATION_DATE);
Json operations = json.at(OPERATIONS);
String brandValue = brandName != null ? brandName.asString() : null;
String sourceValue = sourceVersion != null ? sourceVersion.asString() : null;
String targetValue = targetVersion != null ? targetVersion.asString() : null;
String qualifierValue = qualifier != null ? qualifier.asString() : null;
Date creationValue = creationDate != null ? creationDate.isNull() ? null : new Date(creationDate.asLong()) : null;
Date installationValue = installationDate != null ? installationDate.isNull() ? null : new Date(installationDate.asLong()) : null;
List<PatchOperation> operationsValue = operations != null ? operations.asJsonList().stream().map(PatchOperation::fromJson).collect(Collectors.toList()) : null;
return new PatchInfo(brandValue, sourceValue, targetValue, qualifierValue, creationValue, installationValue, operationsValue);
}
Aggregations