static boolean needsEscapeML(char c) { switch (c) { case '\n': case '\r': case '\t': return false; default: return needsQuotes(c); } } }
char[] valuec=value.toCharArray(); for(char ch : valuec) { if (needsQuotes(ch)) { doEscape=true; break; } JsonValue.isPunctuatorChar(left) || HjsonParser.tryParseNumber(value, true)!=null || startsWithKeyword(value)) { for(char ch : valuec) { if (needsEscape(ch)) { noEscape=false; break; } } if (noEscape) { tw.write(separator+"\""+value+"\""); return; } if (needsEscapeML(ch)) { noEscapeML=false; break; } else if (!HjsonParser.isWhiteSpace(ch)) allWhite=false; if (noEscapeML && !allWhite && !value.contains("'''")) writeMLString(value, tw, level, separator); else tw.write(separator+"\""+JsonWriter.escapeString(value)+"\"");
case OBJECT: JsonObject obj=value.asObject(); if (!noIndent) { if (obj.size()>0) nl(tw, level); else tw.write(separator); } tw.write('{'); nl(tw, level+1); tw.write(escapeName(pair.getName())); tw.write(":"); save(pair.getValue(), tw, level+1, " ", false); if (obj.size()>0) nl(tw, level); tw.write('}'); break; JsonArray arr=value.asArray(); int n=arr.size(); if (!noIndent) { if (n>0) nl(tw, level); else tw.write(separator); } tw.write('['); for (int i=0; i<n; i++) { nl(tw, level+1); save(arr.get(i), tw, level+1, "", true); if (n>0) nl(tw, level); tw.write(']'); break; break; case STRING: writeString(value.asString(), tw, level, separator);
/** * Writes the Hjson representation of this value to the given writer. * <p> * Writing performance can be improved by using a {@link java.io.BufferedWriter BufferedWriter}. * </p> * * @param writer the writer to write this value to * @param options options for the Hjson format * @throws IOException if an I/O error occurs in the writer */ public void writeTo(Writer writer, HjsonOptions options) throws IOException { if (options==null) throw new NullPointerException("options is null"); WritingBuffer buffer=new WritingBuffer(writer, 128); new HjsonWriter(options).save(this, buffer, 0, "", true); buffer.flush(); }
void writeMLString(String value, Writer tw, int level, String separator) throws IOException { String[] lines=value.replace("\r", "").split("\n", -1); if (lines.length==1) { tw.write(separator+"'''"); tw.write(lines[0]); tw.write("'''"); } else { level++; nl(tw, level); tw.write("'''"); for (String line : lines) { nl(tw, line.length()>0?level:0); tw.write(line); } nl(tw, level); tw.write("'''"); } }
char[] valuec=value.toCharArray(); for(char ch : valuec) { if (needsQuotes(ch)) { doEscape=true; break; } JsonValue.isPunctuatorChar(left) || HjsonParser.tryParseNumber(value, true)!=null || startsWithKeyword(value)) { for(char ch : valuec) { if (needsEscape(ch)) { noEscape=false; break; } } if (noEscape) { tw.write(separator+"\""+value+"\""); return; } if (needsEscapeML(ch)) { noEscapeML=false; break; } else if (!HjsonParser.isWhiteSpace(ch)) allWhite=false; if (noEscapeML && !allWhite && !value.contains("'''")) writeMLString(value, tw, level, separator); else tw.write(separator+"\""+JsonWriter.escapeString(value)+"\"");
case OBJECT: JsonObject obj=value.asObject(); if (!noIndent) { if (obj.size()>0) nl(tw, level); else tw.write(separator); } tw.write('{'); nl(tw, level+1); tw.write(escapeName(pair.getName())); tw.write(":"); save(pair.getValue(), tw, level+1, " ", false); if (obj.size()>0) nl(tw, level); tw.write('}'); break; JsonArray arr=value.asArray(); int n=arr.size(); if (!noIndent) { if (n>0) nl(tw, level); else tw.write(separator); } tw.write('['); for (int i=0; i<n; i++) { nl(tw, level+1); save(arr.get(i), tw, level+1, "", true); if (n>0) nl(tw, level); tw.write(']'); break; break; case STRING: writeString(value.asString(), tw, level, separator);
/** * Writes the Hjson representation of this value to the given writer. * <p> * Writing performance can be improved by using a {@link java.io.BufferedWriter BufferedWriter}. * </p> * * @param writer the writer to write this value to * @param options options for the Hjson format * @throws IOException if an I/O error occurs in the writer */ public void writeTo(Writer writer, HjsonOptions options) throws IOException { if (options==null) throw new NullPointerException("options is null"); WritingBuffer buffer=new WritingBuffer(writer, 128); new HjsonWriter(options).save(this, buffer, 0, "", true); buffer.flush(); }
void writeMLString(String value, Writer tw, int level, String separator) throws IOException { String[] lines=value.replace("\r", "").split("\n", -1); if (lines.length==1) { tw.write(separator+"'''"); tw.write(lines[0]); tw.write("'''"); } else { level++; nl(tw, level); tw.write("'''"); for (String line : lines) { nl(tw, line.length()>0?level:0); tw.write(line); } nl(tw, level); tw.write("'''"); } }
/** * Writes the JSON/Hjson representation of this value to the given writer using the given formatting. * <p> * Writing performance can be improved by using a {@link java.io.BufferedWriter BufferedWriter}. * </p> * * @param writer the writer to write this value to * @param format controls the formatting * @throws IOException if an I/O error occurs in the writer */ public void writeTo(Writer writer, Stringify format) throws IOException { WritingBuffer buffer=new WritingBuffer(writer, 128); switch (format) { case PLAIN: new JsonWriter(false).save(this, buffer, 0); break; case FORMATTED: new JsonWriter(true).save(this, buffer, 0); break; case HJSON: new HjsonWriter(null).save(this, buffer, 0, "", true); break; } buffer.flush(); }
static boolean needsEscape(char c) { switch (c) { case '\"': case '\\': return true; default: return needsQuotes(c); } }
/** * Writes the JSON/Hjson representation of this value to the given writer using the given formatting. * <p> * Writing performance can be improved by using a {@link java.io.BufferedWriter BufferedWriter}. * </p> * * @param writer the writer to write this value to * @param format controls the formatting * @throws IOException if an I/O error occurs in the writer */ public void writeTo(Writer writer, Stringify format) throws IOException { WritingBuffer buffer=new WritingBuffer(writer, 128); switch (format) { case PLAIN: new JsonWriter(false).save(this, buffer, 0); break; case FORMATTED: new JsonWriter(true).save(this, buffer, 0); break; case HJSON: new HjsonWriter(null).save(this, buffer, 0, "", true); break; } buffer.flush(); }
static boolean needsEscape(char c) { switch (c) { case '\"': case '\\': return true; default: return needsQuotes(c); } }
static boolean needsEscapeML(char c) { switch (c) { case '\n': case '\r': case '\t': return false; default: return needsQuotes(c); } } }