public JsonObject getAsJsonObject() { if (jsonObject == null) { jsonObject = Utils.fromJson(jsonSerializedState, JsonObject.class); } return jsonObject; }
@SuppressWarnings("unchecked") public static List<String> getAffinityNames(ComputeDescriptionService.ComputeDescription desc) { if (desc.customProperties == null) { return Collections.emptyList(); } String affinitiesAsString = desc.customProperties .getOrDefault(CUSTOM_PROP_NAME_AFFINITY, ""); return Utils.fromJson(affinitiesAsString, List.class); }
public QueryElementResult<T> result(Object json, long count) { QueryElementResult<T> r = new QueryElementResult<>(); r.result = Utils.fromJson(json, this.type); r.documentSelfLink = r.result.documentSelfLink; r.count = count; return r; }
@Override public void updateServiceDocument(ServiceDocument serviceDocument) { super.updateServiceDocument(serviceDocument); this.commonDescription = Utils.fromJson(componentJson, CommonReservationResourceDescription.class); }
private void visitPlacementHostSelectionTaskStage(TaskServiceStageWithLink stage) { if (stage.taskSubStage.equals(DefaultSubStage.COMPLETED.name())) { info.hostSelections = Utils.fromJson(stage.properties, PlacementHostSelectionTaskState.class).hostSelections; } } }
public boolean accepts(TaskServiceStageWithLink stage) { if (stage.documentSelfLink.startsWith(RequestBrokerFactoryService.SELF_LINK) && stage.taskSubStage.equals(RequestBrokerState.SubStage.COMPLETED.name())) { RequestBrokerState state = Utils.fromJson(stage.properties, RequestBrokerState.class); return ResourceType.CONTAINER_TYPE.getName().equals(state.resourceType); } return false; }
private boolean isMigrationFinishNotice(Operation patch) { if (patch.getBodyRaw() instanceof String) { MigrationFinishedNotice doc = Utils.fromJson(patch.getBodyRaw(), MigrationFinishedNotice.class); return MigrationFinishedNotice.KIND.equals(doc.documentKind); } return patch.getBodyRaw() instanceof MigrationFinishedNotice; }
public static HashMap<String, ExampleServiceState> queryResultToExampleState( ServiceDocumentQueryResult r) { HashMap<String, ExampleServiceState> state = new HashMap<>(); for (String k : r.documents.keySet()) { state.put(k, Utils.fromJson(r.documents.get(k), ExampleServiceState.class)); } return state; }
public FindForLink(ServiceDocument document) { this.selfLink = document.documentSelfLink; String s = Utils.toJson(document); this.json = Utils.fromJson(s, JsonObject.class); this.documentClass = document.getClass().getName(); }
@SuppressWarnings("unchecked") public static <T> void assertSerializationPreservesObjects(T instance, BiFunction<T, T, Void> comp) { String json = Utils.toJson(instance); T deserialized = (T) Utils.fromJson(json, instance.getClass()); comp.apply(instance, deserialized); }
@Test public void testParseZonedDateTime() throws Exception { Calendar cal = Calendar.getInstance(); cal.setTimeZone(TimeZone.getTimeZone("Australia/Sydney")); cal.set(2013, 4, 30, 23, 38, 27); cal.set(Calendar.MILLISECOND, 85); ZoneId zid = ZoneId.of("Australia/Sydney"); ZonedDateTime expected = ZonedDateTime.ofInstant(cal.toInstant(), zid); ZonedDateTime actual = Utils.fromJson( "\"2013-05-30T23:38:27.085+10:00[Australia/Sydney]\"", ZonedDateTime.class); assertEquals(expected, actual); }
private void handleEventStreamError(Operation op, ServerSentEvent event) { String errorMsg = String.format("Service %s returned error for %s. id %d", op.getUri(), op.getAction(), op.getId()); ServiceErrorResponse rsp = Utils.fromJson(event.data, ServiceErrorResponse.class); errorMsg += " message " + rsp.message; op.setBodyNoCloning(rsp); op.fail(new ProtocolException(errorMsg)); }
@Test public void testParseJsonWhenMapWithValueTypeObject() throws Exception { TestKeyObjectValueHolder testHolder = Utils.fromJson( "{\"keyValues\":{\"prop1\":\"value1\"}}", TestKeyObjectValueHolder.class); assertEquals("value1", testHolder.keyValues.get("prop1")); }
@Test public void testFactoryPostWithoutId() throws Throwable { ServiceDocumentQueryResult res = createInstances(SERVICE_COUNT, true); assertTrue(res.documentLinks.size() == SERVICE_COUNT); assertTrue(res.documentLinks.size() == res.documents.size()); for (Object s : res.documents.values()) { TenantService.TenantState state = Utils.fromJson(s, TenantService.TenantState.class); assertNotNull(state.id); } }
private String waitForStatus(VerificationHost peer, Function<SynchronizationManagementState.Status, Boolean> f) { SynchronizationManagementState[] state = new SynchronizationManagementState[1]; URI serviceUri = UriUtils.buildUri(peer, SynchronizationManagementService.class); peer.waitFor("Wait for the required status failed", () -> { Operation op = this.sender.sendAndWait(Operation.createGet(serviceUri)); ServiceDocumentQueryResult result = op.getBody(ServiceDocumentQueryResult.class); state[0] = Utils.fromJson(result.documents.get(ExampleService.FACTORY_LINK), SynchronizationManagementState.class); return f.apply(state[0].status); }); return state[0].owner; }
@Override public void handlePost(Operation postOperation) { MigrationTaskService.TransformRequest request = postOperation.getBody(MigrationTaskService.TransformRequest.class); ExampleServiceState state = Utils.fromJson(request.originalDocument, ExampleServiceState.class); state.name = state.name + "-transformed-v2"; MigrationTaskService.TransformResponse response = new MigrationTaskService.TransformResponse(); response.destinationLinks = new HashMap<>(); response.destinationLinks.put(Utils.toJson(state), request.destinationLink); postOperation.setBody(response).complete(); } }
private String waitForStatus(VerificationHost peer, Function<SynchronizationManagementState.Status, Boolean> f) { SynchronizationManagementState[] state = new SynchronizationManagementState[1]; URI serviceUri = UriUtils.buildUri(peer, SynchronizationManagementService.class); peer.waitFor("Wait for the required status failed", () -> { Operation op = this.sender.sendAndWait(Operation.createGet(serviceUri)); ServiceDocumentQueryResult result = op.getBody(ServiceDocumentQueryResult.class); state[0] = Utils.fromJson(result.documents.get(ExampleService.FACTORY_LINK), SynchronizationManagementState.class); return f.apply(state[0].status); }); return state[0].owner; }
@Test public void testCustomConfigurationForClass() { SomeDocument1 doc = new SomeDocument1(); doc.value = new SomeComplexObject("fred", "barney"); String json = Utils.toJson(doc); JsonElement expected = readJson("{ \"value\": \"fred|barney\" " + BORING_JSON_DOC_BITS + "}"); assertEquals(expected, readJson(json)); SomeDocument1 docBack = Utils.fromJson(json, SomeDocument1.class); assertComplexObjectEquals(doc.value, docBack.value); }
@Test public void testCustomConfigurationForClass() { SomeDocument1 doc = new SomeDocument1(); doc.value = new SomeComplexObject("fred", "barney"); String json = Utils.toJson(doc); JsonElement expected = readJson("{ \"value\": \"fred|barney\" " + BORING_JSON_DOC_BITS + "}"); assertEquals(expected, readJson(json)); SomeDocument1 docBack = Utils.fromJson(json, SomeDocument1.class); assertComplexObjectEquals(doc.value, docBack.value); }
@Test public void testCustomConfigurationParaemeterizedType() { SomeDocument2<SomeComplexObject> doc = new SomeDocument2<SomeComplexObject>(); doc.value = new SomeComplexObject("fred", "barney"); String json = Utils.toJson(doc); JsonElement expected = readJson("{ \"value\": \"barney&fred\" " + BORING_JSON_DOC_BITS + "}"); assertEquals(expected, readJson(json)); Type someDocType2 = new TypeToken<SomeDocument2<SomeComplexObject>>() {}.getType(); SomeDocument2<SomeComplexObject> docBack = Utils.fromJson(json, someDocType2); assertComplexObjectEquals(doc.value, docBack.value); }