io.datakernel.codec.json.JsonUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of datakernel-codec Show documentation
Show all versions of datakernel-codec Show documentation
Tools for encoding/decoding of primitives and objects.
/*
* Copyright (C) 2015-2018 SoftIndex LLC.
*
* 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 io.datakernel.codec.json;
import com.google.gson.internal.Streams;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;
import io.datakernel.codec.*;
import io.datakernel.common.parse.ParseException;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
public class JsonUtils {
public static T fromJson(StructuredDecoder decoder, String string) throws ParseException {
JsonReader reader = new JsonReader(new StringReader(string));
T result = decoder.decode(new JsonStructuredInput(reader));
try {
if (reader.peek() != JsonToken.END_DOCUMENT) {
throw new ParseException("Json data was not fully consumed when decoding");
}
} catch (IOException e) {
throw new AssertionError();
}
return result;
}
private static void toJson(StructuredEncoder encoder, T value, Writer writer) {
JsonWriterEx jsonWriter = new JsonWriterEx(writer);
jsonWriter.setLenient(true);
jsonWriter.setIndentEx("");
jsonWriter.setHtmlSafe(false);
jsonWriter.setSerializeNulls(true);
encoder.encode(new JsonStructuredOutput(jsonWriter), value);
}
public static String toJson(StructuredEncoder super T> encoder, T value) {
StringWriter writer = new StringWriter();
toJson(encoder, value, writer);
return writer.toString();
}
public static void toJson(StructuredEncoder super T> encoder, T value, Appendable appendable) {
toJson(encoder, value, Streams.writerForAppendable(appendable));
}
public static StructuredCodec oneline(StructuredCodec codec) {
return indent(codec, "");
}
public static StructuredCodec indent(StructuredCodec codec, String indent) {
return new StructuredCodec() {
@Override
public void encode(StructuredOutput out, T item) {
if (out instanceof JsonStructuredOutput) {
JsonStructuredOutput jsonOut = ((JsonStructuredOutput) out);
if (jsonOut.writer instanceof JsonWriterEx) {
JsonWriterEx jsonWriterEx = (JsonWriterEx) jsonOut.writer;
String previousIndent = jsonWriterEx.getIndentEx();
jsonWriterEx.setIndentEx(indent);
if (indent.isEmpty()) {
try {
jsonWriterEx.writer.write('\n');
} catch (IOException e) {
throw new AssertionError();
}
}
codec.encode(out, item);
jsonWriterEx.setIndentEx(previousIndent);
return;
}
}
codec.encode(out, item);
}
@Override
public T decode(StructuredInput in) throws ParseException {
return codec.decode(in);
}
};
}
public static final class JsonWriterEx extends JsonWriter {
final Writer writer;
public JsonWriterEx(Writer writer) {
super(writer);
this.writer = writer;
}
private String indentEx;
public final void setIndentEx(String indent) {
this.indentEx = indent;
setIndent(indent);
}
@Override
public JsonWriter name(String name) throws IOException {
return super.name(name);
}
public final String getIndentEx() {
return indentEx;
}
}
}