com.github.rschmitt.dynamicobject.internal.EdnSerialization Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of dynamic-object Show documentation
Show all versions of dynamic-object Show documentation
Lightweight data modeling for Java, powered by Clojure.
package com.github.rschmitt.dynamicobject.internal;
import static com.github.rschmitt.dynamicobject.internal.ClojureStuff.Deref;
import static com.github.rschmitt.dynamicobject.internal.ClojureStuff.Eval;
import static com.github.rschmitt.dynamicobject.internal.ClojureStuff.PreferMethod;
import static com.github.rschmitt.dynamicobject.internal.ClojureStuff.PrintMethod;
import static com.github.rschmitt.dynamicobject.internal.ClojureStuff.ReadString;
import static com.github.rschmitt.dynamicobject.internal.ClojureStuff.SimpleDispatch;
import static java.lang.String.format;
import java.io.IOException;
import java.io.PushbackReader;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Iterator;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.BiFunction;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import com.github.rschmitt.dynamicobject.DynamicObject;
import com.github.rschmitt.dynamicobject.EdnTranslator;
import com.github.rschmitt.dynamicobject.Unknown;
import clojure.java.api.Clojure;
import clojure.lang.AFn;
import clojure.lang.IPersistentMap;
public class EdnSerialization {
static {
String clojureCode =
"(defmethod print-method com.github.rschmitt.dynamicobject.internal.DynamicObjectPrintHook " +
"[o, ^java.io.Writer w]" +
"(com.github.rschmitt.dynamicobject.internal.EdnSerialization/invokePrintMethod o w))";
Eval.invoke(ReadString.invoke(clojureCode));
PreferMethod.invoke(PrintMethod, DynamicObjectPrintHook.class, IPersistentMap.class);
PreferMethod.invoke(PrintMethod, DynamicObjectPrintHook.class, Map.class);
clojureCode =
"(defmethod clojure.pprint/simple-dispatch com.github.rschmitt.dynamicobject.internal.DynamicObjectPrintHook " +
"[o] " +
"(com.github.rschmitt.dynamicobject.internal.EdnSerialization/invokePrettyPrint o))";
Eval.invoke(ReadString.invoke(clojureCode));
PreferMethod.invoke(SimpleDispatch, DynamicObjectPrintHook.class, IPersistentMap.class);
PreferMethod.invoke(SimpleDispatch, DynamicObjectPrintHook.class, Map.class);
}
public static class DynamicObjectPrintMethod extends AFn {
@Override
public Object invoke(Object arg1, Object arg2) {
DynamicObject dynamicObject = (DynamicObject) arg1;
Writer writer = (Writer) arg2;
String tag = recordTagCache.getOrDefault(dynamicObject.getType(), null);
try {
if (tag != null) {
writer.write("#");
writer.write(tag);
}
ClojureStuff.PrOn.invoke(dynamicObject.getMap(), writer);
} catch (IOException ex) {
throw new RuntimeException(ex);
}
return null;
}
}
public static class DynamicObjectPrettyPrint extends AFn {
@Override
public Object invoke(Object arg1) {
Object arg2 = Deref.invoke(Clojure.var("clojure.core/*out*"));
DynamicObject dynamicObject = (DynamicObject) arg1;
Writer writer = (Writer) arg2;
String tag = recordTagCache.getOrDefault(dynamicObject.getType(), null);
try {
if (tag != null) {
writer.write("#");
writer.write(tag);
}
SimpleDispatch.invoke(dynamicObject.getMap());
} catch (IOException ex) {
throw new RuntimeException(ex);
}
return null;
}
}
private static final DynamicObjectPrettyPrint dynamicObjectPrettyPrint = new DynamicObjectPrettyPrint();
private static final DynamicObjectPrintMethod dynamicObjectPrintMethod = new DynamicObjectPrintMethod();
private static final AtomicReference
© 2015 - 2024 Weber Informatics LLC | Privacy Policy