This repository has been archived by the owner on Jan 27, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
from_json()
/to_json()
Fix functions.
- Loading branch information
1 parent
bec6763
commit fc4fd5b
Showing
7 changed files
with
410 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
metafix/src/main/java/org/metafacture/metafix/JsonValue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* Copyright 2022 hbz NRW | ||
* | ||
* Licensed under the Apache License, Version 2.0 the "License"; | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.metafacture.metafix; | ||
|
||
import com.fasterxml.jackson.core.JsonFactory; | ||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import com.fasterxml.jackson.core.SerializableString; | ||
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
import java.io.IOException; | ||
import java.io.StringWriter; | ||
import java.io.UncheckedIOException; | ||
|
||
// TODO: Utilize JsonDecoder/JsonEncoder instead? | ||
|
||
@FunctionalInterface | ||
public interface JsonValue { | ||
|
||
void toJson(JsonGenerator jsonGenerator); | ||
|
||
default String toJson() throws IOException { | ||
return toJson(false); | ||
} | ||
|
||
default String toJson(final boolean prettyPrinting) throws IOException { | ||
final StringWriter writer = new StringWriter(); | ||
final JsonGenerator jsonGenerator = new JsonFactory().createGenerator(writer); | ||
jsonGenerator.setPrettyPrinter(prettyPrinting ? new DefaultPrettyPrinter((SerializableString) null) : null); | ||
|
||
try { | ||
toJson(jsonGenerator); | ||
} | ||
catch (final UncheckedIOException e) { | ||
throw e.getCause(); | ||
} | ||
|
||
jsonGenerator.flush(); | ||
|
||
return writer.toString(); | ||
} | ||
|
||
class Parser { | ||
|
||
private static final ObjectMapper MAPPER = new ObjectMapper(); | ||
|
||
public Parser() { | ||
} | ||
|
||
public Value parse(final String source) throws IOException { | ||
return parse(MAPPER.readTree(source)); | ||
} | ||
|
||
private Value parse(final JsonNode node) { | ||
final Value value; | ||
|
||
if (node.isObject()) { | ||
value = Value.newHash(h -> node.fields().forEachRemaining(e -> h.put(e.getKey(), parse(e.getValue())))); | ||
} | ||
else if (node.isArray()) { | ||
value = Value.newArray(a -> node.elements().forEachRemaining(v -> a.add(parse(v)))); | ||
} | ||
else { | ||
value = new Value(node.textValue()); | ||
} | ||
|
||
return value; | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.