@Test public void testJSONSerialization() throws IOException { OpenAPI openAPI = ParserTest.parse(helper, "/openapi-greeting.yml", OpenAPISupport.OpenAPIMediaType.YAML); Writer writer = new StringWriter(); Serializer.serialize(helper.types(), implsToTypes, openAPI, OpenApiSerializer.Format.JSON, writer); JsonStructure json = TestUtil.jsonFromReader(new StringReader(writer.toString())); assertThat(json.getValue("/x-my-personal-map/owner/last").toString(), is("\"Myself\"")); JsonValue otherItem = json.getValue("/x-other-item"); assertThat(otherItem.getValueType(), is(JsonValue.ValueType.NUMBER)); assertThat(Double.valueOf(otherItem.toString()), is(10.0)); JsonValue seq = json.getValue("/info/x-my-personal-seq"); assertThat(seq.getValueType(), is(JsonValue.ValueType.ARRAY)); JsonArray seqArray = seq.asJsonArray(); JsonValue first = seqArray.get(0); assertThat(first.getValueType(), is(JsonValue.ValueType.OBJECT)); JsonObject firstObj = first.asJsonObject(); checkJsonPathStringValue(firstObj, "/who", "Prof. Plum"); checkJsonPathStringValue(firstObj, "/why", "felt like it"); JsonValue second = seqArray.get(1); assertThat(second.getValueType(), is(JsonValue.ValueType.OBJECT)); JsonObject secondObj = second.asJsonObject(); checkJsonPathStringValue(secondObj, "/when", "yesterday"); checkJsonPathStringValue(secondObj, "/how", "with the lead pipe"); JsonValue xInt = json.getValue("/x-int"); assertThat(xInt.getValueType(), is(JsonValue.ValueType.NUMBER)); assertThat(Integer.valueOf(xInt.toString()), is(117)); JsonValue xBoolean = json.getValue("/x-boolean"); assertThat(xBoolean.getValueType(), is(JsonValue.ValueType.TRUE)); JsonValue xStrings = json.getValue("/x-string-array"); assertThat(xStrings.getValueType(), is(JsonValue.ValueType.ARRAY)); JsonArray xStringArray = xStrings.asJsonArray(); assertThat(xStringArray.size(), is(2)); checkJsonStringValue(xStringArray.get(0), "one"); checkJsonStringValue(xStringArray.get(1), "two"); JsonValue xObjects = json.getValue("/x-object-array"); assertThat(xObjects.getValueType(), is(JsonValue.ValueType.ARRAY)); JsonArray xObjectArray = xObjects.asJsonArray(); assertThat(xObjectArray.size(), is(2)); first = xObjectArray.get(0); assertThat(first.getValueType(), is(JsonValue.ValueType.OBJECT)); firstObj = first.asJsonObject(); checkJsonPathStringValue(firstObj, "/name", "item-1"); checkJsonPathIntValue(firstObj, "/value", 16); second = xObjectArray.get(1); assertThat(second.getValueType(), is(JsonValue.ValueType.OBJECT)); secondObj = second.asJsonObject(); checkJsonPathStringValue(secondObj, "/name", "item-2"); checkJsonPathIntValue(secondObj, "/value", 18); }
static void serialize(Map<Class<?>, ExpandedTypeDescription> types, Map<Class<?>, ExpandedTypeDescription> implsToTypes, OpenAPI openAPI, OpenApiSerializer.Format fmt, Writer writer) { if (fmt == OpenApiSerializer.Format.JSON) { serialize(types, implsToTypes, openAPI, writer, JSON_DUMPER_OPTIONS, DumperOptions.ScalarStyle.DOUBLE_QUOTED); } else { serialize(types, implsToTypes, openAPI, writer, YAML_DUMPER_OPTIONS, DumperOptions.ScalarStyle.PLAIN); } }
Serializer { static void serialize(Map<Class<?>, ExpandedTypeDescription> types, Map<Class<?>, ExpandedTypeDescription> implsToTypes, OpenAPI openAPI, OpenApiSerializer.Format fmt, Writer writer) { if (fmt == OpenApiSerializer.Format.JSON) { serialize(types, implsToTypes, openAPI, writer, JSON_DUMPER_OPTIONS, DumperOptions.ScalarStyle.DOUBLE_QUOTED); } else { serialize(types, implsToTypes, openAPI, writer, YAML_DUMPER_OPTIONS, DumperOptions.ScalarStyle.PLAIN); } } }
Serializer { static void serialize(Map<Class<?>, ExpandedTypeDescription> types, Map<Class<?>, ExpandedTypeDescription> implsToTypes, OpenAPI openAPI, OpenApiSerializer.Format fmt, Writer writer) { if (fmt == OpenApiSerializer.Format.JSON) { serialize(types, implsToTypes, openAPI, writer, JSON_DUMPER_OPTIONS, DumperOptions.ScalarStyle.DOUBLE_QUOTED); } else { serialize(types, implsToTypes, openAPI, writer, YAML_DUMPER_OPTIONS, DumperOptions.ScalarStyle.PLAIN); } } private  Serializer(); }
Serializer { static void serialize(Map<Class<?>, ExpandedTypeDescription> types, Map<Class<?>, ExpandedTypeDescription> implsToTypes, OpenAPI openAPI, OpenApiSerializer.Format fmt, Writer writer) { if (fmt == OpenApiSerializer.Format.JSON) { serialize(types, implsToTypes, openAPI, writer, JSON_DUMPER_OPTIONS, DumperOptions.ScalarStyle.DOUBLE_QUOTED); } else { serialize(types, implsToTypes, openAPI, writer, YAML_DUMPER_OPTIONS, DumperOptions.ScalarStyle.PLAIN); } } private  Serializer();  }
Serializer { static void serialize(Map<Class<?>, ExpandedTypeDescription> types, Map<Class<?>, ExpandedTypeDescription> implsToTypes, OpenAPI openAPI, OpenApiSerializer.Format fmt, Writer writer) { if (fmt == OpenApiSerializer.Format.JSON) { serialize(types, implsToTypes, openAPI, writer, JSON_DUMPER_OPTIONS, DumperOptions.ScalarStyle.DOUBLE_QUOTED); } else { serialize(types, implsToTypes, openAPI, writer, YAML_DUMPER_OPTIONS, DumperOptions.ScalarStyle.PLAIN); } } private  Serializer();   }