Gson JSON 最大尺寸
Gson JSON max size
我需要从 Clob 中提取一些数据并将其序列化为 JSON 格式。
Gson 可以处理的最大大小是多少?
在这里https://github.com/google/gson/blob/master/UserGuide.md我可以找到
"Strings: Deserialized strings of over 25MB without any problems"
上下文:我用..
ResultSet.getClob()
-> BufferedReader
-> String singleLine
-> StringBuilder
-> String "jsonAttribute" to serialize
更详细:
try{
StringBuilder sb = new StringBuilder();
BufferedReader br = new BufferedReader( resultset.getClob(2).getCharacterStream() );
String line;
try{
while ((line = br.readLine()) != null) {
sb.append(line).append("\n");
}
}catch(IOException ee){
// logger
throw ee;
}
String jsonAttribute = sb.toString();
}catch(Exception xx){..}
注意:在我当前的代码中,限制是 Integer.MAX_VALUE
我的解决方案是使用从数据库中检索到的数据块。我想知道 GSON 可以处理的理论最大尺寸。我不会在接收方使用浏览器。
Gson 不做任何限制。它也没有任何已知的架构限制。
我认为您的方法将面临的主要问题是首先将数据加载到内存中。
我建议使用 Gson stream API 在从数据库中读取时写入 JSON。使用 JsonWriter
并创建和流式传输您的 JSON 对象。
Reader reader = resultset.getClob(2).getCharacterStream();
JsonWriter jsonWriter = new JsonWriter(someOutputStream);
copyStream(reader, someOutputStream);
其中 copyStream
可能类似于
public static void copyStream(Reader istream, Writer ostream) throws IOException {
char buffer[] = new char[2048];
while (true) {
int len = istream.read(buffer);
if (len == -1)
return;
ostream.write(buffer, 0, len);
}
}
我确认@sargue 的回答。我尝试了以下 test 并且工作得很好,分配给 jvm 的堆内存量合适。
@Test
public void testGsonLimitss(){
EntityHalConverter<HugeData> converter = new EntityHalConverter<>(HugeData.class);
HugeData hugeData = new HugeData();
converter.toJson( hugeData );
}
class HugeData implements HalResource {
String big1, big2;
public HugeData(){
// big1 = StringUtils.repeat("X", 400000000); // 300 millions chars ~ approx 3 mb. With multibyte chars ..... 3.5 mb
big2 = StringUtils.repeat("Y", Integer.MAX_VALUE-10);
}
}
这是我正在使用的转换器(带有 Halarious)..
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonParseException;
import ch.halarious.core.HalResource;
import ch.halarious.core.HalSerializer;
import ch.halarious.core.HalDeserializer;
import ch.halarious.core.HalExclusionStrategy;
public class EntityHalConverter <T> {
private Gson gson;
private GsonBuilder builder;
private Class<T> paramType;
/* HalConverter<ProgrammeData> halConverter = new HalConverter<>(ProgrammeData.class);
*/
public EntityHalConverter(Class<T> paramType) {
builder = new GsonBuilder();
builder.setExclusionStrategies(new HalExclusionStrategy());
this.paramType = paramType;
}
/* String jsonResult = halConverter.toJson( programmeData );
*/
public String toJson( T result ) throws JsonParseException{
builder.registerTypeAdapter(HalResource.class, new HalSerializer());
gson = builder.create();
return gson.toJson(result, HalResource.class);
}
/* ProgrammeData pcsProg = halConverter.convert( jsonString );
*/
public T fromJson( String json ) throws JsonParseException {
builder.registerTypeAdapter( HalResource.class, new HalDeserializer(paramType) );
gson = builder.create();
return (T)gson.fromJson( json, HalResource.class );
}
}
我需要从 Clob 中提取一些数据并将其序列化为 JSON 格式。
Gson 可以处理的最大大小是多少?
在这里https://github.com/google/gson/blob/master/UserGuide.md我可以找到 "Strings: Deserialized strings of over 25MB without any problems"
上下文:我用..
ResultSet.getClob()
-> BufferedReader
-> String singleLine
-> StringBuilder
-> String "jsonAttribute" to serialize
更详细:
try{
StringBuilder sb = new StringBuilder();
BufferedReader br = new BufferedReader( resultset.getClob(2).getCharacterStream() );
String line;
try{
while ((line = br.readLine()) != null) {
sb.append(line).append("\n");
}
}catch(IOException ee){
// logger
throw ee;
}
String jsonAttribute = sb.toString();
}catch(Exception xx){..}
注意:在我当前的代码中,限制是 Integer.MAX_VALUE
我的解决方案是使用从数据库中检索到的数据块。我想知道 GSON 可以处理的理论最大尺寸。我不会在接收方使用浏览器。
Gson 不做任何限制。它也没有任何已知的架构限制。
我认为您的方法将面临的主要问题是首先将数据加载到内存中。
我建议使用 Gson stream API 在从数据库中读取时写入 JSON。使用 JsonWriter
并创建和流式传输您的 JSON 对象。
Reader reader = resultset.getClob(2).getCharacterStream();
JsonWriter jsonWriter = new JsonWriter(someOutputStream);
copyStream(reader, someOutputStream);
其中 copyStream
可能类似于
public static void copyStream(Reader istream, Writer ostream) throws IOException {
char buffer[] = new char[2048];
while (true) {
int len = istream.read(buffer);
if (len == -1)
return;
ostream.write(buffer, 0, len);
}
}
我确认@sargue 的回答。我尝试了以下 test 并且工作得很好,分配给 jvm 的堆内存量合适。
@Test
public void testGsonLimitss(){
EntityHalConverter<HugeData> converter = new EntityHalConverter<>(HugeData.class);
HugeData hugeData = new HugeData();
converter.toJson( hugeData );
}
class HugeData implements HalResource {
String big1, big2;
public HugeData(){
// big1 = StringUtils.repeat("X", 400000000); // 300 millions chars ~ approx 3 mb. With multibyte chars ..... 3.5 mb
big2 = StringUtils.repeat("Y", Integer.MAX_VALUE-10);
}
}
这是我正在使用的转换器(带有 Halarious)..
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonParseException;
import ch.halarious.core.HalResource;
import ch.halarious.core.HalSerializer;
import ch.halarious.core.HalDeserializer;
import ch.halarious.core.HalExclusionStrategy;
public class EntityHalConverter <T> {
private Gson gson;
private GsonBuilder builder;
private Class<T> paramType;
/* HalConverter<ProgrammeData> halConverter = new HalConverter<>(ProgrammeData.class);
*/
public EntityHalConverter(Class<T> paramType) {
builder = new GsonBuilder();
builder.setExclusionStrategies(new HalExclusionStrategy());
this.paramType = paramType;
}
/* String jsonResult = halConverter.toJson( programmeData );
*/
public String toJson( T result ) throws JsonParseException{
builder.registerTypeAdapter(HalResource.class, new HalSerializer());
gson = builder.create();
return gson.toJson(result, HalResource.class);
}
/* ProgrammeData pcsProg = halConverter.convert( jsonString );
*/
public T fromJson( String json ) throws JsonParseException {
builder.registerTypeAdapter( HalResource.class, new HalDeserializer(paramType) );
gson = builder.create();
return (T)gson.fromJson( json, HalResource.class );
}
}