|
@@ -0,0 +1,91 @@
|
|
|
+using Invercargill;
|
|
|
+namespace InvercargillJson {
|
|
|
+
|
|
|
+ public class JsonlInputStream : Enumerable<JsonElement>, JsonElements, Elements {
|
|
|
+
|
|
|
+ private DataInputStream base_stream { get; set; }
|
|
|
+
|
|
|
+ public JsonlInputStream(InputStream stream) {
|
|
|
+ if(stream is DataInputStream) {
|
|
|
+ base_stream = (DataInputStream)stream;
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ base_stream = new DataInputStream(stream);
|
|
|
+ }
|
|
|
+
|
|
|
+ public JsonlInputStream.from_file(File file) throws Error {
|
|
|
+ base_stream = new DataInputStream(file.read());
|
|
|
+ }
|
|
|
+
|
|
|
+ public override Invercargill.Tracker<Element> get_tracker () {
|
|
|
+ return new AdvanceTracker<Element> ((out i) => {
|
|
|
+ try {
|
|
|
+ i = read_next();
|
|
|
+ return i != null;
|
|
|
+ }
|
|
|
+ catch (Error e) {
|
|
|
+ warning (@"Premature end of JsonlInputStream due to error: $(e.message)");
|
|
|
+ i = null;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ public JsonElement? read_next() throws Error {
|
|
|
+ var str = base_stream.read_line();
|
|
|
+ if(str == null)
|
|
|
+ {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ return new JsonElement.from_string(str);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void close() throws Error {
|
|
|
+ base_stream.close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public class JsonlOutputStream {
|
|
|
+
|
|
|
+ private DataOutputStream base_stream { get; set; }
|
|
|
+
|
|
|
+ public JsonlOutputStream(OutputStream stream) {
|
|
|
+ if(stream is DataOutputStream) {
|
|
|
+ base_stream = (DataOutputStream)stream;
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ base_stream = new DataOutputStream(stream);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void write(Element element) throws Error {
|
|
|
+ var json_element = (element is JsonElement) ? (JsonElement)element : new JsonElement.from_element(element);
|
|
|
+ base_stream.put_string(json_element.stringify(false) + "\n");
|
|
|
+ }
|
|
|
+
|
|
|
+ public void write_all(Enumerable<Element> elements) throws Error {
|
|
|
+ foreach (var element in elements) {
|
|
|
+ write(element);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void write_object(Properties object) throws Error {
|
|
|
+ write(new NativeElement<Properties>(object));
|
|
|
+ }
|
|
|
+
|
|
|
+ public void write_all_objects(Enumerable<Properties> objects) throws Error {
|
|
|
+ foreach (var object in objects) {
|
|
|
+ write_object(object);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void flush() throws Error {
|
|
|
+ base_stream.flush();
|
|
|
+ }
|
|
|
+
|
|
|
+ public void close() throws Error {
|
|
|
+ base_stream.close();
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+}
|