Skip to content

Commit b72c4e2

Browse files
committed
Implement Record.toJson().
1 parent 04dd7c8 commit b72c4e2

File tree

4 files changed

+98
-1
lines changed

4 files changed

+98
-1
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ subprojects {
3434
versions = [
3535
'ace': '1.3.3',
3636
'equalsverifier': '3.8.2',
37+
'jackson': '2.13.3',
3738
'jetty': '9.4.14.v20181114',
3839
'jquery': '3.3.1-1',
3940
'junit_jupiter': '5.8.2',

metafix/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ def passSystemProperties = {
1212
dependencies {
1313
implementation "org.eclipse.xtext:org.eclipse.xtext:${versions.xtext}"
1414
implementation "org.eclipse.xtext:org.eclipse.xtext.xbase:${versions.xtext}"
15+
implementation "com.fasterxml.jackson.core:jackson-core:${versions.jackson}"
1516
implementation "com.google.guava:guava:${versions.guava}"
1617
implementation "org.slf4j:slf4j-api:${versions.slf4j}"
1718

metafix/src/main/java/org/metafacture/metafix/Record.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@
1919
import org.metafacture.metafix.FixPath.InsertMode;
2020
import org.metafacture.metafix.Value.TypeMatcher;
2121

22+
import com.fasterxml.jackson.core.JsonFactory;
23+
import com.fasterxml.jackson.core.JsonGenerator;
24+
import com.fasterxml.jackson.core.SerializableString;
25+
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
26+
27+
import java.io.IOException;
28+
import java.io.StringWriter;
29+
import java.io.UncheckedIOException;
2230
import java.util.Collection;
2331
import java.util.Deque;
2432
import java.util.LinkedHashMap;
@@ -110,6 +118,27 @@ public String toString() {
110118
return super.toString();
111119
}
112120

121+
public String toJson() throws IOException {
122+
return toJson(false);
123+
}
124+
125+
public String toJson(final boolean prettyPrinting) throws IOException {
126+
final StringWriter writer = new StringWriter();
127+
final JsonGenerator jsonGenerator = new JsonFactory().createGenerator(writer);
128+
jsonGenerator.setPrettyPrinter(prettyPrinting ? new DefaultPrettyPrinter((SerializableString) null) : null);
129+
130+
try {
131+
toJson(jsonGenerator);
132+
}
133+
catch (final UncheckedIOException e) {
134+
throw e.getCause();
135+
}
136+
137+
jsonGenerator.flush();
138+
139+
return writer.toString();
140+
}
141+
113142
/**
114143
* Retrieves the field value from this record. Falls back to retrieving the
115144
* <i>virtual</i> field if the field name is not already

metafix/src/main/java/org/metafacture/metafix/Value.java

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
import org.metafacture.commons.tries.SimpleRegexTrie;
2020
import org.metafacture.commons.tries.WildcardTrie;
2121

22+
import com.fasterxml.jackson.core.JsonGenerator;
23+
24+
import java.io.IOException;
25+
import java.io.UncheckedIOException;
2226
import java.util.ArrayList;
2327
import java.util.Collection;
2428
import java.util.ConcurrentModificationException;
@@ -44,7 +48,7 @@
4448
* Represents a record value, i.e., either an {@link Array}, a {@link Hash},
4549
* or a {@link String}.
4650
*/
47-
public class Value {
51+
public class Value { // checkstyle-disable-line ClassDataAbstractionCoupling
4852

4953
private static final String FIELD_PATH_SEPARATOR = "\\.";
5054

@@ -278,6 +282,31 @@ public String toString() {
278282
);
279283
}
280284

285+
private void toJson(final JsonGenerator jsonGenerator) {
286+
if (isNull()) {
287+
try {
288+
jsonGenerator.writeNull();
289+
}
290+
catch (final IOException e) {
291+
throw new UncheckedIOException(e);
292+
}
293+
}
294+
else {
295+
matchType()
296+
.ifArray(a -> a.toJson(jsonGenerator))
297+
.ifHash(h -> h.toJson(jsonGenerator))
298+
.ifString(s -> {
299+
try {
300+
jsonGenerator.writeString(s);
301+
}
302+
catch (final IOException e) {
303+
throw new UncheckedIOException(e);
304+
}
305+
})
306+
.orElseThrow();
307+
}
308+
}
309+
281310
/*package-private*/ static String[] split(final String fieldPath) {
282311
return fieldPath.split(FIELD_PATH_SEPARATOR);
283312
}
@@ -379,6 +408,8 @@ private abstract static class AbstractValueType {
379408
@Override
380409
public abstract String toString();
381410

411+
protected abstract void toJson(JsonGenerator jsonGenerator);
412+
382413
}
383414

384415
/**
@@ -448,6 +479,18 @@ public String toString() {
448479
return list.toString();
449480
}
450481

482+
@Override
483+
protected void toJson(final JsonGenerator jsonGenerator) {
484+
try {
485+
jsonGenerator.writeStartArray();
486+
forEach(v -> v.toJson(jsonGenerator));
487+
jsonGenerator.writeEndArray();
488+
}
489+
catch (final IOException e) {
490+
throw new UncheckedIOException(e);
491+
}
492+
}
493+
451494
public void remove(final int index) {
452495
list.remove(index);
453496
}
@@ -712,6 +755,29 @@ public String toString() {
712755
return map.toString();
713756
}
714757

758+
@Override
759+
protected void toJson(final JsonGenerator jsonGenerator) {
760+
try {
761+
jsonGenerator.writeStartObject();
762+
763+
forEach((f, v) -> {
764+
try {
765+
jsonGenerator.writeFieldName(f);
766+
}
767+
catch (final IOException e) {
768+
throw new UncheckedIOException(e);
769+
}
770+
771+
v.toJson(jsonGenerator);
772+
});
773+
774+
jsonGenerator.writeEndObject();
775+
}
776+
catch (final IOException e) {
777+
throw new UncheckedIOException(e);
778+
}
779+
}
780+
715781
/**
716782
* Avoids {@link ConcurrentModificationException} when modifying the hash based on matched fields.
717783
*

0 commit comments

Comments
 (0)