Skip to content

Commit f15a8a0

Browse files
committed
Support input charset and output charset. #5
1 parent 5022284 commit f15a8a0

2 files changed

Lines changed: 16 additions & 8 deletions

File tree

src/main/java/com/dashjoin/jsonata/cli/Main.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.io.PrintStream;
1010
import java.io.PrintWriter;
1111
import java.io.StringWriter;
12+
import java.nio.charset.Charset;
1213
import java.nio.file.Files;
1314
import java.util.Map;
1415

@@ -44,16 +45,17 @@ void initCli() {
4445
options = new Options();
4546
options.addOption("v", "version", false, "Display version info");
4647
options.addOption("h", "help", false, "Display help and version info");
47-
// TODO options.addOption("j", "jsonargs", true, "Specify arguments as JSON object");
4848
options.addOption(Option.builder("e").longOpt("expression")
4949
.argName("file")
5050
.hasArg()
5151
.desc("JSONata expression file to evaluate")
5252
//.required()
5353
.build());
5454
options.addOption("i", "input", true, "JSON input file (- for stdin)");
55+
options.addOption("ic", "icharset", true, "Input character set (default=UTF-8)");
5556
options.addOption("f", "format", true, "Input format (default=auto)");
5657
options.addOption("o", "output", true, "JSON output file (default=stdout)");
58+
options.addOption("oc", "ocharset", true, "Output character set (default=UTF-8)");
5759
options.addOption("time", false, "Print performance timers to stderr");
5860
options.addOption("c", "compact", false, "Compact JSON output (don't prettify)");
5961
options.addOption(Option.builder("b").longOpt("bindings").
@@ -108,6 +110,9 @@ void run(String[] args) throws Throwable {
108110
bindingsStr = cmd.getOptionValue("b");
109111
Map<String, Object> bindingsObj = bindingsStr != null ? (Map<String, Object>)Json.parseJson(bindingsStr) : null;
110112

113+
String icharset = cmd.getOptionValue("ic", "UTF-8");
114+
String ocharset = cmd.getOptionValue("oc", "UTF-8");
115+
111116
InputStream in = null;
112117
Object input = null;
113118
{
@@ -132,15 +137,15 @@ void run(String[] args) throws Throwable {
132137
out = new FileOutputStream(arg);
133138
}
134139

135-
PrintStream pout = new PrintStream(out);
140+
PrintStream pout = new PrintStream(out, false, ocharset);
136141

137142
long t0 = System.currentTimeMillis();
138143

139144
String formatStr = cmd.getOptionValue("f", TerminalUtil.InputFormat.auto.name());
140145
TerminalUtil.InputFormat format = TerminalUtil.InputFormat.valueOf(formatStr);
141146

142147
if (in!=null)
143-
input = TerminalUtil.readInput(in, format);
148+
input = TerminalUtil.readInput(in, format, Charset.forName(icharset));
144149

145150
long t1 = System.currentTimeMillis();
146151

src/main/java/com/dashjoin/jsonata/cli/TerminalUtil.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import java.io.InputStream;
66
import java.io.InputStreamReader;
77
import java.io.UnsupportedEncodingException;
8+
import java.nio.charset.Charset;
9+
810
import com.dashjoin.jsonata.json.Json;
911

1012
/**
@@ -37,11 +39,12 @@ public static enum InputFormat {
3739
*
3840
* @param in
3941
* @param format
42+
* @param cs
4043
* @return Input object
4144
* @throws IOException
4245
* @throws UnsupportedEncodingException
4346
*/
44-
public static Object readInput(InputStream in, InputFormat format)
47+
public static Object readInput(InputStream in, InputFormat format, Charset cs)
4548
throws IOException, UnsupportedEncodingException {
4649
Object input = null;
4750
switch (format) {
@@ -51,19 +54,19 @@ public static Object readInput(InputStream in, InputFormat format)
5154
try {
5255
// First try JSON, if there is an error,
5356
// reset the stream and use string format
54-
return readInput(in, InputFormat.json);
57+
return readInput(in, InputFormat.json, cs);
5558
} catch (Exception ex) {
5659
if (in.markSupported()) {
5760
in.reset();
58-
return readInput(in, InputFormat.string);
61+
return readInput(in, InputFormat.string, cs);
5962
}
6063
// We cannot reset the stream: throw the exception
6164
// Need to specify the format manually in this case
6265
throw ex;
6366
}
6467
}
6568
case json: {
66-
input = Json.parseJson(new InputStreamReader(in));
69+
input = Json.parseJson(new InputStreamReader(in, cs));
6770
break;
6871
}
6972
case string: {
@@ -72,7 +75,7 @@ public static Object readInput(InputStream in, InputFormat format)
7275
for (int length; (length = in.read(buffer)) != -1;) {
7376
result.write(buffer, 0, length);
7477
}
75-
input = result.toString("UTF-8");
78+
input = result.toString(cs);
7679
break;
7780
}
7881
default:

0 commit comments

Comments
 (0)