-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathIO.java
More file actions
167 lines (148 loc) · 7.33 KB
/
IO.java
File metadata and controls
167 lines (148 loc) · 7.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*******************************************************************************
* Copyright (c) 2009-2013 CWI All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v1.0 which accompanies this
* distribution, and is available at http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
*
* * Jurgen J. Vinju - Jurgen.Vinju@cwi.nl - CWI
* * Mark Hills - Mark.Hills@cwi.nl (CWI)
* * Arnold - Lankamp - Arnold.Lankamp@cwi.nl
* * Bert Lisser - Bert.Lisser@cwi.nl
*******************************************************************************/
package org.rascalmpl.library.lang.json;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.nio.charset.Charset;
import java.util.Map;
import java.util.stream.Collectors;
import org.rascalmpl.debug.IRascalMonitor;
import org.rascalmpl.exceptions.RuntimeExceptionFactory;
import org.rascalmpl.library.lang.json.internal.JsonValueReader;
import org.rascalmpl.library.lang.json.internal.JsonValueWriter;
import org.rascalmpl.types.ReifiedType;
import org.rascalmpl.types.TypeReifier;
import org.rascalmpl.uri.URIResolverRegistry;
import org.rascalmpl.values.IRascalValueFactory;
import org.rascalmpl.values.functions.IFunction;
import io.usethesource.vallang.IBool;
import io.usethesource.vallang.IConstructor;
import io.usethesource.vallang.IInteger;
import io.usethesource.vallang.IMap;
import io.usethesource.vallang.ISourceLocation;
import io.usethesource.vallang.IString;
import io.usethesource.vallang.ITuple;
import io.usethesource.vallang.IValue;
import io.usethesource.vallang.type.Type;
import io.usethesource.vallang.type.TypeStore;
import com.google.gson.stream.JsonWriter;
public class IO {
private final IRascalValueFactory values;
private final IRascalMonitor monitor;
public IO(IRascalValueFactory values, IRascalMonitor monitor) {
super();
this.values = values;
this.monitor = monitor;
}
private IValue doReadJSON(Reader in,
IValue type, ISourceLocation loc, IString dateTimeFormat, IBool lenient, IBool trackOrigins,
IFunction parsers, IMap nulls, IBool explicitConstructorNames, IBool explicitDataTypes) throws IOException {
TypeStore store = new TypeStore();
Type start = new TypeReifier(values).valueToType((IConstructor) type, store);
if (parsers.getType() instanceof ReifiedType && parsers.getType().getTypeParameters().getFieldType(0).isTop()) {
// ignore the default parser
parsers = null;
}
try {
return new JsonValueReader(values, store, monitor, loc)
.setCalendarFormat(dateTimeFormat.getValue())
.setLenient(lenient.getValue())
.setParsers(parsers)
.setNulls(unreify(nulls))
.setExplicitConstructorNames(explicitConstructorNames.getValue())
.setExplicitDataTypes(explicitDataTypes.getValue())
.setTrackOrigins(trackOrigins.getValue())
.read(in, start);
}
catch (NullPointerException e) {
throw RuntimeExceptionFactory.io("NPE in error handling code");
}
}
public IValue readJSON(
IValue type, ISourceLocation loc, IString dateTimeFormat, IBool lenient, IBool trackOrigins,
IFunction parsers, IMap nulls, IBool explicitConstructorNames, IBool explicitDataTypes) {
try (Reader in = URIResolverRegistry.getInstance().getCharacterReader(loc)) {
return doReadJSON(in, type, loc, dateTimeFormat, lenient, trackOrigins, parsers, nulls, explicitConstructorNames, explicitDataTypes);
}
catch (IOException e) {
throw RuntimeExceptionFactory.io(e);
}
}
public IValue parseJSON(IValue type, IString src, IString dateTimeFormat, IBool lenient, IBool trackOrigins,
IFunction parsers, IMap nulls, IBool explicitConstructorNames, IBool explicitDataTypes) {
try (Reader in = new StringReader(src.getValue())) {
return doReadJSON(in, type, null, dateTimeFormat, lenient, trackOrigins, parsers, nulls, explicitConstructorNames, explicitDataTypes);
}
catch (IOException e) {
throw RuntimeExceptionFactory.io(e);
}
}
public void writeJSON(ISourceLocation loc, IValue value, IBool unpackedLocations, IString dateTimeFormat,
IBool dateTimeAsInt, IBool rationalsAsString, IInteger indent, IBool dropOrigins, IFunction formatter, IBool explicitConstructorNames,
IBool explicitDataTypes, IBool fileLocationsAsPathOnly) {
try (JsonWriter out =
new JsonWriter(new OutputStreamWriter(URIResolverRegistry.getInstance().getOutputStream(loc, false),
Charset.forName("UTF8")))) {
if (indent.intValue() > 0) {
out.setIndent(" ".substring(0, indent.intValue() % 9));
}
new JsonValueWriter()
.setCalendarFormat(dateTimeFormat.getValue())
.setDatesAsInt(dateTimeAsInt.getValue())
.setRationalsAsString(rationalsAsString.getValue())
.setUnpackedLocations(unpackedLocations.getValue())
.setDropOrigins(dropOrigins.getValue())
.setFormatters(formatter)
.setExplicitConstructorNames(explicitConstructorNames.getValue())
.setExplicitDataTypes(explicitDataTypes.getValue())
.setFileLocationsAsPathOnly(fileLocationsAsPathOnly.getValue())
.write(out, value);
}
catch (IOException e) {
throw RuntimeExceptionFactory.io(e);
}
}
public IString asJSON(IValue value, IBool unpackedLocations, IString dateTimeFormat, IBool dateTimeAsInt, IBool rationalsAsString,
IInteger indent, IBool dropOrigins, IFunction formatter, IBool explicitConstructorNames,
IBool explicitDataTypes, IBool fileLocationsAsPathOnly) {
StringWriter string = new StringWriter();
try (JsonWriter out = new JsonWriter(string)) {
if (indent.intValue() > 0) {
out.setIndent(" ".substring(0, indent.intValue() % 9));
}
new JsonValueWriter()
.setCalendarFormat(dateTimeFormat.getValue())
.setDatesAsInt(dateTimeAsInt.getValue())
.setRationalsAsString(rationalsAsString.getValue())
.setUnpackedLocations(unpackedLocations.getValue())
.setDropOrigins(dropOrigins.getValue())
.setFormatters(formatter)
.setExplicitConstructorNames(explicitConstructorNames.getValue())
.setExplicitDataTypes(explicitDataTypes.getValue())
.setFileLocationsAsPathOnly(fileLocationsAsPathOnly.getValue())
.write(out, value);
return values.string(string.toString());
}
catch (IOException e) {
throw RuntimeExceptionFactory.io(e);
}
}
private Map<Type, IValue> unreify(IMap nulls) {
var tr = new TypeReifier(values);
return nulls.stream().map(t -> (ITuple) t)
.collect(Collectors.toMap(t -> tr.valueToType((IConstructor) t.get(0)), t -> t.get(1)));
}
}