使用JSON字符串编写器而不是StringWriter将MustacheJava模板直接编译为JSON
我希望使用编写器将MustacheJava模板直接编译为JSON,编写器编写一个转义JSON字符串,而不是使用StringWriter然后执行toString 这是我目前的代码-使用JSON字符串编写器而不是StringWriter将MustacheJava模板直接编译为JSON,java,json,gson,mustache,Java,Json,Gson,Mustache,我希望使用编写器将MustacheJava模板直接编译为JSON,编写器编写一个转义JSON字符串,而不是使用StringWriter然后执行toString 这是我目前的代码- MustacheFactory mustacheFactory = new DefaultMustacheFactory(); // reader is my InputStreamReader pointing to my template file Mustache mustache = mustacheFact
MustacheFactory mustacheFactory = new DefaultMustacheFactory();
// reader is my InputStreamReader pointing to my template file
Mustache mustache = mustacheFactory.compile(reader, "example");
StringWriter stringWriter = new StringWriter();
mustache.execute(stringWriter, view);
String html = stringWriter.toString();
// elementAdapter is a TypeAdapter<JsonElement>
elementAdapter.write(out, new JsonPrimitive(html));
JsonStringWriter的伪实现是-
public class JsonStringWriter extends Writer
{
Writer writer;
public JsonStringValueWriter(PrintWriter writer)
{
this.writer = writer;
}
@Override
public void write(char[] cbuf, int off, int len)
{
// check cbuf for string escape characters
// if found, write escape to underlying writer
// otherwise, write cbuf to underlying writer
}
}
我不想写这门课。我的问题是什么JSON API目前有这个类,您能提供对它的引用吗?谢谢
public class JsonStringWriter extends Writer
{
Writer writer;
public JsonStringValueWriter(PrintWriter writer)
{
this.writer = writer;
}
@Override
public void write(char[] cbuf, int off, int len)
{
// check cbuf for string escape characters
// if found, write escape to underlying writer
// otherwise, write cbuf to underlying writer
}
}