Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core mgmt, SubResource implements JsonSerializable to support azure-json #40076

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,17 @@

package com.azure.core.management;

import com.azure.json.JsonReader;
import com.azure.json.JsonSerializable;
import com.azure.json.JsonToken;
import com.azure.json.JsonWriter;

import java.io.IOException;

/**
* The SubResource model.
*/
public class SubResource {
public class SubResource implements JsonSerializable<SubResource> {
/**
* Resource Id.
*/
Expand Down Expand Up @@ -37,4 +44,35 @@ public SubResource withId(String id) {
this.id = id;
return this;
}

@Override
public JsonWriter toJson(JsonWriter jsonWriter) throws IOException {
return jsonWriter.writeStartObject().writeStringField("id", id).writeEndObject();
}

/**
* Reads a JSON stream into a {@link SubResource}.
*
* @param jsonReader The {@link JsonReader} being read.
* @return The {@link SubResource} that the JSON stream represented, may return null.
* @throws IOException If a {@link SubResource} fails to be read from the {@code jsonReader}.
*/
public static SubResource fromJson(JsonReader jsonReader) throws IOException {
return jsonReader.readObject(reader -> {
SubResource subResource = new SubResource();

while (reader.nextToken() != JsonToken.END_OBJECT) {
String fieldName = reader.getFieldName();
reader.nextToken();

if ("id".equals(fieldName)) {
subResource.id = reader.getString();
} else {
reader.skipChildren();
}
}

return subResource;
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.azure.core.management.implementation.ProxyResourceAccessHelper;
import com.azure.json.JsonProviders;
import com.azure.json.JsonReader;
import com.azure.json.JsonSerializable;
import com.azure.json.JsonToken;
import com.azure.json.JsonWriter;
import com.azure.json.ReadValueCallback;
Expand All @@ -14,6 +15,10 @@
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.io.StringWriter;
import java.util.Collections;
import java.util.List;
import java.util.UUID;

public class ResourceTests {

Expand Down Expand Up @@ -127,6 +132,102 @@ public static ResourceWithSystemData fromJson(JsonReader jsonReader) throws IOEx
}
}

private static class SubResourceResource extends SubResource {
private SubResource subResource;
private List<SubResource> subResourceList;
private SubResourceResource subResourceResource;
private List<SubResourceResource> subResourceResourceList;

public SubResource subResource() {
return subResource;
}

public SubResourceResource withSubResource(SubResource subResource) {
this.subResource = subResource;
return this;
}

public List<SubResource> subResourceList() {
return subResourceList;
}

public SubResourceResource withSubResourceList(List<SubResource> subResourceList) {
this.subResourceList = subResourceList;
return this;
}

public SubResourceResource subResourceResource() {
return subResourceResource;
}

public SubResourceResource withSubResourceResource(SubResourceResource subResourceResource) {
this.subResourceResource = subResourceResource;
return this;
}

public List<SubResourceResource> subResourceResourceList() {
return subResourceResourceList;
}

public SubResourceResource withSubResourceResourceList(List<SubResourceResource> subResourceResourceList) {
this.subResourceResourceList = subResourceResourceList;
return this;
}

@Override
public SubResourceResource withId(String id) {
super.withId(id);
return this;
}

@Override
public JsonWriter toJson(JsonWriter jsonWriter) throws IOException {
return jsonWriter.writeStartObject()
.writeStringField("id", id())
.writeJsonField("subResource", this.subResource)
.writeArrayField("subResourceList", this.subResourceList, JsonWriter::writeJson)
.writeJsonField("subResourceResource", this.subResourceResource)
.writeArrayField("subResourceResourceList", this.subResourceResourceList, JsonWriter::writeJson)
.writeEndObject();
}

/**
* Reads a JSON stream into a {@link SubResourceResource}.
*
* @param jsonReader The {@link JsonReader} being read.
* @return The {@link SubResourceResource} that the JSON stream represented, may return null.
* @throws IOException If a {@link SubResourceResource} fails to be read from the {@code jsonReader}.
*/
public static SubResourceResource fromJson(JsonReader jsonReader) throws IOException {
return jsonReader.readObject(reader -> {
SubResourceResource subResource = new SubResourceResource();

while (reader.nextToken() != JsonToken.END_OBJECT) {
String fieldName = reader.getFieldName();
reader.nextToken();

if ("id".equals(fieldName)) {
subResource.withId(reader.getString());
} else if ("subResource".equals(fieldName)) {
subResource.withSubResource(reader.readObject(reader1 -> SubResource.fromJson(reader1)));
} else if ("subResourceList".equals(fieldName)) {
subResource.withSubResourceList(reader.readArray(reader1 -> SubResource.fromJson(reader1)));
} else if ("subResourceResource".equals(fieldName)) {
subResource.withSubResourceResource(
reader.readObject(reader1 -> SubResourceResource.fromJson(reader1)));
} else if ("subResourceResourceList".equals(fieldName)) {
subResource.withSubResourceResourceList(
reader.readArray(reader1 -> SubResourceResource.fromJson(reader1)));
} else {
reader.skipChildren();
}
}

return subResource;
});
}
}

@Test
public void testSerialization() throws IOException {
String cosmosAccountJson
Expand Down Expand Up @@ -190,6 +291,33 @@ public void testSerialization() throws IOException {
Assertions.assertEquals("Microsoft.KeyVault/vaults", vaultResource.type());
Assertions.assertEquals(0, vaultResource.tags().size());
Assertions.assertNull(vaultResource.systemData());

// test SubResource
SubResourceResource subResourceResourceRoot = new SubResourceResource();
SubResource subResource = new SubResource().withId(UUID.randomUUID().toString());
SubResourceResource subResourceResourceNest = new SubResourceResource().withId(UUID.randomUUID().toString());
subResourceResourceRoot.withSubResource(subResource)
.withSubResourceList(Collections.singletonList(subResource))
.withSubResourceResource(subResourceResourceNest)
.withSubResourceResourceList(Collections.singletonList(subResourceResourceNest));

String json = serializeToString(subResourceResourceRoot);
SubResourceResource subResourceResourceRootDeserialized = deserialize(json, SubResourceResource::fromJson);
Assertions.assertEquals(subResource.id(), subResourceResourceRootDeserialized.subResource().id());
Assertions.assertEquals(subResource.id(),
subResourceResourceRootDeserialized.subResourceList().iterator().next().id());
Assertions.assertEquals(subResourceResourceNest.id(),
subResourceResourceRootDeserialized.subResourceResource().id());
Assertions.assertEquals(subResourceResourceNest.id(),
subResourceResourceRootDeserialized.subResourceResourceList().iterator().next().id());
}

private static <T extends JsonSerializable<T>> String serializeToString(T serializable) throws IOException {
StringWriter writer = new StringWriter();
JsonWriter jsonWriter = JsonProviders.createWriter(writer);
serializable.toJson(jsonWriter);
jsonWriter.flush();
return writer.toString();
}

private static <T> T deserialize(String json, ReadValueCallback<JsonReader, T> reader) throws IOException {
Expand Down