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

Tentative fix to deal with issues using generateJSONWithStreaming #247

Merged
merged 9 commits into from
Aug 16, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ class JsonGenerator {
root: Node,
withIds: IdentityHashMap<Node, String>? = null,
withOriginIds: IdentityHashMap<Node, String>? = null,
withDestinationIds: IdentityHashMap<Node, String>? = null
withDestinationIds: IdentityHashMap<Node, String>? = null,
shortClassNames: Boolean = false
): JsonElement {
return nodeToJson(
root, shortClassNames,
Expand Down Expand Up @@ -130,27 +131,28 @@ class JsonGenerator {
if (result.root == null) {
writer.nullValue()
} else {
result.root.toJsonStreaming(writer, shortClassNames)
generateJSONWithStreaming(result.root, writer, shortClassNames)
}
writer.endObject()
}

fun generateJSONWithStreaming(root: Node, writer: JsonWriter, shortClassNames: Boolean = false) {
root.toJsonStreaming(writer, shortClassNames)
val gson = gsonBuilder.setPrettyPrinting().create()
gson.toJson(generateJSON(root, null, null, null, shortClassNames), writer)
gabriele-tomassetti marked this conversation as resolved.
Show resolved Hide resolved
}

fun generateString(root: Node, withIds: IdentityHashMap<Node, String>? = null): String {
val gson = GsonBuilder().setPrettyPrinting().create()
val gson = gsonBuilder.setPrettyPrinting().create()
return gson.toJson(generateJSON(root, withIds))
}

fun generateString(result: Result<out Node>, withIds: IdentityHashMap<Node, String>? = null): String {
val gson = GsonBuilder().setPrettyPrinting().create()
val gson = gsonBuilder.setPrettyPrinting().create()
return gson.toJson(generateJSON(result, withIds))
}

fun generateString(result: ParsingResult<out Node>, withIds: IdentityHashMap<Node, String>? = null): String {
val gson = GsonBuilder().setPrettyPrinting().create()
val gson = gsonBuilder.setPrettyPrinting().create()
return gson.toJson(generateJSON(result, withIds))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,9 @@ class JsonGenerationTest {
assertEquals(
"""{"#type":"com.strumenta.kolasu.serialization.MyRoot",
|"mainSection":{"#type":"com.strumenta.kolasu.serialization.Section","contents":
|[{"#type":"com.strumenta.kolasu.serialization.Content","annidatedContent":null,"id":1},
|{"#type":"com.strumenta.kolasu.serialization.Content","annidatedContent":
|{"#type":"com.strumenta.kolasu.serialization.Content","annidatedContent":
|{"#type":"com.strumenta.kolasu.serialization.Content","annidatedContent":null,"id":4},"id":3},"id":2}],
|[{"#type":"com.strumenta.kolasu.serialization.Content","id":1},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be useful to add several examples, using the flag shortClassNames set to false and to true

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would also be useful to have tests to load the examples, both with shortClassNames set to false and to true

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added more examples for generation, still working on deserialization tests.

|{"#type":"com.strumenta.kolasu.serialization.Content","annidatedContent":{"#type":"com.strumenta.kolasu.serialization.Content",
|"annidatedContent":{"#type":"com.strumenta.kolasu.serialization.Content","id":4},"id":3},"id":2}],
|"name":"Section1"},"otherSections":[]}
""".trimMargin().replace("\n", ""),
json,
Expand Down
Loading