|
1 | 1 | /******************************************************************************* |
2 | | - * Copyright (c) 2009-2013 CWI |
3 | | - * All rights reserved. This program and the accompanying materials |
4 | | - * are made available under the terms of the Eclipse Public License v1.0 |
5 | | - * which accompanies this distribution, and is available at |
6 | | - * http://www.eclipse.org/legal/epl-v10.html |
| 2 | + * Copyright (c) 2009-2013 CWI All rights reserved. This program and the accompanying materials are |
| 3 | + * made available under the terms of the Eclipse Public License v1.0 which accompanies this |
| 4 | + * distribution, and is available at http://www.eclipse.org/legal/epl-v10.html |
7 | 5 | * |
8 | 6 | * Contributors: |
9 | | -
|
10 | | - * * Jurgen J. Vinju - Jurgen.Vinju@cwi.nl - CWI |
11 | | - * * Mark Hills - Mark.Hills@cwi.nl (CWI) |
12 | | - * * Arnold Lankamp - Arnold.Lankamp@cwi.nl |
13 | | - * * Bert Lisser - Bert.Lisser@cwi.nl |
| 7 | + * |
| 8 | + * * Jurgen J. Vinju - Jurgen.Vinju@cwi.nl - CWI * Mark Hills - Mark.Hills@cwi.nl (CWI) * Arnold |
| 9 | + * Lankamp - Arnold.Lankamp@cwi.nl * Bert Lisser - Bert.Lisser@cwi.nl |
14 | 10 | *******************************************************************************/ |
15 | 11 | package org.rascalmpl.library.lang.json; |
16 | 12 |
|
|
47 | 43 | import com.google.gson.stream.JsonWriter; |
48 | 44 |
|
49 | 45 | public class IO { |
50 | | - private final IRascalValueFactory values; |
51 | | - private final IRascalMonitor monitor; |
52 | | - |
53 | | - public IO(IRascalValueFactory values, IRascalMonitor monitor) { |
54 | | - super(); |
55 | | - this.values = values; |
56 | | - this.monitor = monitor; |
57 | | - } |
58 | | - |
59 | | - public IValue readJSON(IValue type, ISourceLocation loc, IString dateTimeFormat, IBool lenient, IBool trackOrigins, IFunction parsers, IMap nulls, IBool explicitConstructorNames, IBool explicitDataTypes) { |
60 | | - TypeStore store = new TypeStore(); |
61 | | - Type start = new TypeReifier(values).valueToType((IConstructor) type, store); |
62 | | - |
63 | | - if (parsers.getType() instanceof ReifiedType && parsers.getType().getTypeParameters().getFieldType(0).isTop()) { |
64 | | - // ignore the default parser |
65 | | - parsers = null; |
66 | | - } |
67 | | - |
68 | | - try (JsonReader in = new JsonReader(URIResolverRegistry.getInstance().getCharacterReader(loc))) { |
69 | | - in.setLenient(lenient.getValue()); |
70 | | - return new JsonValueReader(values, store, monitor, trackOrigins.getValue() ? loc : null) |
71 | | - .setCalendarFormat(dateTimeFormat.getValue()) |
72 | | - .setParsers(parsers) |
73 | | - .setNulls(unreify(nulls)) |
74 | | - .setExplicitConstructorNames(explicitConstructorNames.getValue()) |
75 | | - .setExplicitDataTypes(explicitDataTypes.getValue()) |
76 | | - .read(in, start); |
77 | | - } |
78 | | - catch (IOException e) { |
79 | | - throw RuntimeExceptionFactory.io(values.string(e.getMessage()), null, null); |
80 | | - } |
81 | | - catch (NullPointerException e) { |
82 | | - throw RuntimeExceptionFactory.io(values.string("NPE in error handling code"), null, null); |
83 | | - } |
| 46 | + private final IRascalValueFactory values; |
| 47 | + private final IRascalMonitor monitor; |
| 48 | + |
| 49 | + public IO(IRascalValueFactory values, IRascalMonitor monitor) { |
| 50 | + super(); |
| 51 | + this.values = values; |
| 52 | + this.monitor = monitor; |
| 53 | + } |
| 54 | + |
| 55 | + public IValue readJSON(IValue type, ISourceLocation loc, IString dateTimeFormat, IBool lenient, IBool trackOrigins, |
| 56 | + IFunction parsers, IMap nulls, IBool explicitConstructorNames, IBool explicitDataTypes) { |
| 57 | + TypeStore store = new TypeStore(); |
| 58 | + Type start = new TypeReifier(values).valueToType((IConstructor) type, store); |
| 59 | + |
| 60 | + if (parsers.getType() instanceof ReifiedType && parsers.getType().getTypeParameters().getFieldType(0).isTop()) { |
| 61 | + // ignore the default parser |
| 62 | + parsers = null; |
| 63 | + } |
| 64 | + |
| 65 | + try (JsonReader in = new JsonReader(URIResolverRegistry.getInstance().getCharacterReader(loc))) { |
| 66 | + in.setLenient(lenient.getValue()); |
| 67 | + return new JsonValueReader(values, store, monitor, trackOrigins.getValue() ? loc : null) |
| 68 | + .setCalendarFormat(dateTimeFormat.getValue()).setParsers(parsers).setNulls(unreify(nulls)) |
| 69 | + .setExplicitConstructorNames(explicitConstructorNames.getValue()) |
| 70 | + .setExplicitDataTypes(explicitDataTypes.getValue()).read(in, start); |
| 71 | + } |
| 72 | + catch (IOException e) { |
| 73 | + throw RuntimeExceptionFactory.io(values.string(e.getMessage()), null, null); |
| 74 | + } |
| 75 | + catch (NullPointerException e) { |
| 76 | + throw RuntimeExceptionFactory.io(values.string("NPE in error handling code"), null, null); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + private Map<Type, IValue> unreify(IMap nulls) { |
| 81 | + var tr = new TypeReifier(values); |
| 82 | + return nulls.stream().map(t -> (ITuple) t) |
| 83 | + .collect(Collectors.toMap(t -> tr.valueToType((IConstructor) t.get(0)), t -> t.get(1))); |
| 84 | + } |
| 85 | + |
| 86 | + public IValue parseJSON(IValue type, IString src, IString dateTimeFormat, IBool lenient, IBool trackOrigins, |
| 87 | + IFunction parsers, IMap nulls, IBool explicitConstructorNames, IBool explicitDataTypes) { |
| 88 | + TypeStore store = new TypeStore(); |
| 89 | + Type start = new TypeReifier(values).valueToType((IConstructor) type, store); |
| 90 | + |
| 91 | + try (JsonReader in = new JsonReader(new StringReader(src.getValue()))) { |
| 92 | + in.setLenient(lenient.getValue()); |
| 93 | + return new JsonValueReader(values, store, monitor, |
| 94 | + trackOrigins.getValue() ? URIUtil.rootLocation("unknown") : null) |
| 95 | + .setCalendarFormat(dateTimeFormat.getValue()).setParsers(parsers).setNulls(unreify(nulls)) |
| 96 | + .setExplicitConstructorNames(explicitConstructorNames.getValue()) |
| 97 | + .setExplicitDataTypes(explicitDataTypes.getValue()).read(in, start); |
| 98 | + } |
| 99 | + catch (IOException e) { |
| 100 | + throw RuntimeExceptionFactory.io(values.string(e.getMessage())); |
| 101 | + } |
| 102 | + catch (NullPointerException e) { |
| 103 | + throw RuntimeExceptionFactory.io(values.string("NPE")); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + public void writeJSON(ISourceLocation loc, IValue value, IBool unpackedLocations, IString dateTimeFormat, |
| 108 | + IBool dateTimeAsInt, IInteger indent, IBool dropOrigins, IFunction formatter, IBool explicitConstructorNames, |
| 109 | + IBool explicitDataTypes) { |
| 110 | + try (JsonWriter out = |
| 111 | + new JsonWriter(new OutputStreamWriter(URIResolverRegistry.getInstance().getOutputStream(loc, false), |
| 112 | + Charset.forName("UTF8")))) { |
| 113 | + if (indent.intValue() > 0) { |
| 114 | + out.setIndent(" ".substring(0, indent.intValue() % 9)); |
| 115 | + } |
| 116 | + |
| 117 | + new JsonValueWriter().setCalendarFormat(dateTimeFormat.getValue()).setDatesAsInt(dateTimeAsInt.getValue()) |
| 118 | + .setUnpackedLocations(unpackedLocations.getValue()).setDropOrigins(dropOrigins.getValue()) |
| 119 | + .setFormatters(formatter).setExplicitConstructorNames(explicitConstructorNames.getValue()) |
| 120 | + .setExplicitDataTypes(explicitDataTypes.getValue()).write(out, value); |
| 121 | + } |
| 122 | + catch (IOException e) { |
| 123 | + throw RuntimeExceptionFactory.io(values.string(e.getMessage()), null, null); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + public IString asJSON(IValue value, IBool unpackedLocations, IString dateTimeFormat, IBool dateTimeAsInt, |
| 128 | + IInteger indent, IBool dropOrigins, IFunction formatter, IBool explicitConstructorNames, |
| 129 | + IBool explicitDataTypes) { |
| 130 | + StringWriter string = new StringWriter(); |
| 131 | + |
| 132 | + try (JsonWriter out = new JsonWriter(string)) { |
| 133 | + if (indent.intValue() > 0) { |
| 134 | + out.setIndent(" ".substring(0, indent.intValue() % 9)); |
| 135 | + } |
| 136 | + new JsonValueWriter().setCalendarFormat(dateTimeFormat.getValue()).setDatesAsInt(dateTimeAsInt.getValue()) |
| 137 | + .setUnpackedLocations(unpackedLocations.getValue()).setDropOrigins(dropOrigins.getValue()) |
| 138 | + .setFormatters(formatter).setExplicitConstructorNames(explicitConstructorNames.getValue()) |
| 139 | + .setExplicitDataTypes(explicitDataTypes.getValue()).write(out, value); |
| 140 | + |
| 141 | + return values.string(string.toString()); |
| 142 | + } |
| 143 | + catch (IOException e) { |
| 144 | + throw RuntimeExceptionFactory.io(values.string(e.getMessage()), null, null); |
| 145 | + } |
84 | 146 | } |
85 | | - |
86 | | - private Map<Type, IValue> unreify(IMap nulls) { |
87 | | - var tr = new TypeReifier(values); |
88 | | - return nulls.stream() |
89 | | - .map(t -> (ITuple) t) |
90 | | - .collect(Collectors.toMap(t -> tr.valueToType((IConstructor) t.get(0)), t -> t.get(1))); |
91 | | - } |
92 | | - |
93 | | - public IValue parseJSON(IValue type, IString src, IString dateTimeFormat, IBool lenient, IBool trackOrigins, IFunction parsers, IMap nulls, IBool explicitConstructorNames, IBool explicitDataTypes) { |
94 | | - TypeStore store = new TypeStore(); |
95 | | - Type start = new TypeReifier(values).valueToType((IConstructor) type, store); |
96 | | - |
97 | | - try (JsonReader in = new JsonReader(new StringReader(src.getValue()))) { |
98 | | - in.setLenient(lenient.getValue()); |
99 | | - return new JsonValueReader(values, store, monitor, trackOrigins.getValue() ? URIUtil.rootLocation("unknown") : null) |
100 | | - .setCalendarFormat(dateTimeFormat.getValue()) |
101 | | - .setParsers(parsers) |
102 | | - .setNulls(unreify(nulls)) |
103 | | - .setExplicitConstructorNames(explicitConstructorNames.getValue()) |
104 | | - .setExplicitDataTypes(explicitDataTypes.getValue()) |
105 | | - .read(in, start); |
106 | | - } |
107 | | - catch (IOException e) { |
108 | | - throw RuntimeExceptionFactory.io(values.string(e.getMessage())); |
109 | | - } |
110 | | - catch (NullPointerException e) { |
111 | | - throw RuntimeExceptionFactory.io(values.string("NPE")); |
112 | | - } |
113 | | - } |
114 | | - |
115 | | - public void writeJSON(ISourceLocation loc, IValue value, IBool unpackedLocations, IString dateTimeFormat, IBool dateTimeAsInt, IInteger indent, IBool dropOrigins, IFunction formatter, IBool explicitConstructorNames, IBool explicitDataTypes) { |
116 | | - try (JsonWriter out = new JsonWriter(new OutputStreamWriter(URIResolverRegistry.getInstance().getOutputStream(loc, false), Charset.forName("UTF8")))) { |
117 | | - if (indent.intValue() > 0) { |
118 | | - out.setIndent(" ".substring(0, indent.intValue() % 9)); |
119 | | - } |
120 | | - |
121 | | - new JsonValueWriter() |
122 | | - .setCalendarFormat(dateTimeFormat.getValue()) |
123 | | - .setDatesAsInt(dateTimeAsInt.getValue()) |
124 | | - .setUnpackedLocations(unpackedLocations.getValue()) |
125 | | - .setDropOrigins(dropOrigins.getValue()) |
126 | | - .setFormatters(formatter) |
127 | | - .setExplicitConstructorNames(explicitConstructorNames.getValue()) |
128 | | - .setExplicitDataTypes(explicitDataTypes.getValue()) |
129 | | - .write(out, value); |
130 | | - } catch (IOException e) { |
131 | | - throw RuntimeExceptionFactory.io(values.string(e.getMessage()), null, null); |
132 | | - } |
133 | | - } |
134 | | - |
135 | | - public IString asJSON(IValue value, IBool unpackedLocations, IString dateTimeFormat, IBool dateTimeAsInt, IInteger indent, IBool dropOrigins, IFunction formatter, IBool explicitConstructorNames, IBool explicitDataTypes) { |
136 | | - StringWriter string = new StringWriter(); |
137 | | - |
138 | | - try (JsonWriter out = new JsonWriter(string)) { |
139 | | - if (indent.intValue() > 0) { |
140 | | - out.setIndent(" ".substring(0, indent.intValue() % 9)); |
141 | | - } |
142 | | - new JsonValueWriter() |
143 | | - .setCalendarFormat(dateTimeFormat.getValue()) |
144 | | - .setDatesAsInt(dateTimeAsInt.getValue()) |
145 | | - .setUnpackedLocations(unpackedLocations.getValue()) |
146 | | - .setDropOrigins(dropOrigins.getValue()) |
147 | | - .setFormatters(formatter) |
148 | | - .setExplicitConstructorNames(explicitConstructorNames.getValue()) |
149 | | - .setExplicitDataTypes(explicitDataTypes.getValue()) |
150 | | - .write(out, value); |
151 | | - |
152 | | - return values.string(string.toString()); |
153 | | - } catch (IOException e) { |
154 | | - throw RuntimeExceptionFactory.io(values.string(e.getMessage()), null, null); |
155 | | - } |
156 | | - } |
157 | 147 | } |
0 commit comments