Skip to content

Commit e5e68b8

Browse files
committed
added JSONPrinter#printSchemaMap() (with unittest), made Schema#describeTo() public and added unittest, using printSchemaMap() in ObjectSchema#describePropertiesTo()
1 parent 32246ea commit e5e68b8

File tree

4 files changed

+33
-15
lines changed

4 files changed

+33
-15
lines changed

core/src/main/java/org/everit/json/schema/ObjectSchema.java

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -467,12 +467,7 @@ void describePropertiesTo(JSONPrinter writer) {
467467
}
468468
if (!propertySchemas.isEmpty()) {
469469
writer.key("properties");
470-
writer.object();
471-
propertySchemas.entrySet().forEach(entry -> {
472-
writer.key(entry.getKey());
473-
entry.getValue().describeTo(writer);
474-
});
475-
writer.endObject();
470+
writer.printSchemaMap(propertySchemas);
476471
}
477472
writer.ifPresent("minProperties", minProperties);
478473
writer.ifPresent("maxProperties", maxProperties);
@@ -488,12 +483,7 @@ void describePropertiesTo(JSONPrinter writer) {
488483
}
489484
if (!patternProperties.isEmpty()) {
490485
writer.key("patternProperties");
491-
writer.object();
492-
patternProperties.entrySet().forEach(entry -> {
493-
writer.key(entry.getKey().toString());
494-
entry.getValue().describeTo(writer);
495-
});
496-
writer.endObject();
486+
writer.printSchemaMap(patternProperties);
497487
}
498488
}
499489

core/src/main/java/org/everit/json/schema/Schema.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,11 +165,16 @@ public String getId() {
165165
}
166166

167167
/**
168-
* <<<<<<< HEAD Describes the instance as a JSONObject to {@code writer}.
168+
* Describes the instance as a JSONObject to {@code writer}.
169+
*
170+
* First it adds the {@code "title} , {@code "description"} and {@code "id"} properties then calls
171+
* {@link #describePropertiesTo(JSONPrinter)}, which will add the subclass-specific properties.
172+
*
173+
* It is used by {@link #toString()} to serialize the schema instance into its JSON representation.
169174
*
170175
* @param writer it will receive the schema description
171176
*/
172-
final void describeTo(final JSONPrinter writer) {
177+
public final void describeTo(final JSONPrinter writer) {
173178
writer.object();
174179
writer.ifPresent("title", title);
175180
writer.ifPresent("description", description);

core/src/main/java/org/everit/json/schema/internal/JSONPrinter.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package org.everit.json.schema.internal;
22

3+
import org.everit.json.schema.Schema;
34
import org.json.JSONWriter;
45

56
import java.io.Writer;
7+
import java.util.HashMap;
8+
import java.util.Map;
69

710
import static java.util.Objects.requireNonNull;
811

@@ -70,4 +73,13 @@ public void ifFalse(String key, Boolean value) {
7073
writer.value(value);
7174
}
7275
}
76+
77+
public <K> void printSchemaMap(Map<K, Schema> input) {
78+
object();
79+
input.entrySet().forEach(entry -> {
80+
key(entry.getKey().toString());
81+
entry.getValue().describeTo(this);
82+
});
83+
endObject();
84+
}
7385
}

core/src/test/java/org/everit/json/schema/internal/JSONPrinterTest.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package org.everit.json.schema.internal;
22

3+
import org.everit.json.schema.NullSchema;
4+
import org.everit.json.schema.Schema;
35
import org.json.JSONObject;
46
import org.json.JSONWriter;
57
import org.junit.Before;
68
import org.junit.Ignore;
79
import org.junit.Test;
810

911
import java.io.StringWriter;
12+
import java.util.HashMap;
1013

1114
import static org.junit.Assert.assertEquals;
1215
import static org.junit.Assert.assertNull;
@@ -106,7 +109,7 @@ public void ifFalseOmits() {
106109
assertNull(actualObj().opt("mykey"));
107110
}
108111

109-
@Test @Ignore
112+
@Test
110113
public void ifFalseHandlesNullAsTrue() {
111114
JSONPrinter subject = subject();
112115
subject.object();
@@ -124,4 +127,12 @@ public void arraySupport() {
124127
assertEquals("[true]", buffer.toString());
125128
}
126129

130+
@Test
131+
public void printSchemaMap() {
132+
HashMap<Number, Schema> input = new HashMap<Number, Schema>();
133+
input.put(2, NullSchema.INSTANCE);
134+
subject().printSchemaMap(input);
135+
assertEquals("{\"2\":"+NullSchema.INSTANCE.toString() + "}", buffer.toString());
136+
}
137+
127138
}

0 commit comments

Comments
 (0)