简体   繁体   English

为什么在 gson.toJSon 时返回一个空对象

[英]Why return an empty object when gson.toJSon

I'm trying to parse a geojson file using streaming of file, because the file could be a huge file.我正在尝试使用文件流解析 geojson 文件,因为该文件可能是一个巨大的文件。 1KB, 20MG or 1 TB. 1KB、20MG 或 1TB。

I parse all file use streaming as you can see in ParseGeojsonStream class.正如您在 ParseGeojsonStream 类中看到的那样,我解析了所有文件使用流。

But when I try to parse to json using gson to storeit in postgressql database, the field property is an empty object.但是当我尝试使用 gson 解析为 json 以将其存储在 postgressql 数据库中时,字段属性是一个空对象。

But I do a log before cast to json as in not empty.但是我在转换为 json 之前做了一个日志,因为它不是空的。

I hope you understand me.我希望你能理解我。

The flux is next:通量是下一个:

  • First I received from a post string properties and an inputStream from my controller.首先,我从我的控制器接收到一个 post 字符串属性和一个 inputStream。
  • Next, my service call ParseGeojosnStream to Stream the content of file.接下来,我的服务调用 ParseGeojosnStream 来流式传输文件的内容。
  • Next, I pass the name and list of features and insert into table.接下来,我传递名称和特征列表并插入到表中。
  • Finally, I create scheme with string properties and name of table as Id.最后,我使用字符串属性和表名作为 Id 创建方案。

So, Why return an empty object of properties when cast to json to store it on database??那么,为什么在转换为 json 以将其存储在数据库中时返回一个空的属性对象?

Here is the code:这是代码:

public class ParseGeojsonStream {

    private static List<Feature> features = new ArrayList<Feature>();

    public static List<Feature> parseJson(InputStream is) throws IOException {

        // Create and configure an ObjectMapper instance
        ObjectMapper mapper = new ObjectMapper();
        mapper.registerModule(new JavaTimeModule());
        mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);

        // Create a JsonParser instance
        try (JsonParser jsonParser = mapper.getFactory().createParser(is)) {

            // Check the first token
            if (jsonParser.nextToken() != JsonToken.START_OBJECT) {
                throw new IllegalStateException("Expected content to be an object");
            }

            JsonParser featureJson = findFeaturesToken(jsonParser);

            if (featureJson == null) {
                throw new IOException("Expected content to be not null");
            }

            if (featureJson.nextToken() != JsonToken.START_ARRAY) {
                throw new IOException("Expected content to be an array");
            }

            // Iterate over the tokens until the end of the array
            while (featureJson.nextToken() != JsonToken.END_ARRAY) {
                // Read a contact instance using ObjectMapper and do something with it
                Feature feature = mapper.readValue(featureJson, Feature.class);
                features.add(feature);
            }
            return features;
        }
    }


    private static JsonParser findFeaturesToken(JsonParser jsonParser) throws IOException {
        JsonParser json = jsonParser;
        while(json.nextToken() != JsonToken.END_OBJECT) {
            String text = json.getText();
            if ( text.equalsIgnoreCase(GEOJSONConstants.ApiJSON.FEATURES) ) {
                return json;
            }
        }
        return null;
    }

}

This store all features, and here is a log of one item: Feature: Feature(properties={ELEMENTO=null}, geometry={type=MultiPolygon, coordinates=[-3.691344883619331, 40.424004164131894] })这存储了所有特征,这里是一个条目的日志: Feature: Feature(properties={ELEMENTO=null}, geometry={type=MultiPolygon, coordinates=[-3.691344883619331, 40.424004164131894] })

Feature Class:特征类:

@Data 
public class Feature {

    private Object properties;
    private Object geometry;
}

Method from TableGeojsonStream:来自 TableGeojsonStream 的方法:

public String createTable(final String name, final List<Feature> features) {
        Gson gson = new Gson();
        checkedNameTable= nameTable(name);

        jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS " + checkedNameTable + " ( table_id SERIAL, properties jsonb not null, geom geometry(GeometryZ,4326), primary key (table_id));");
        for (int i=0; i<1; i++ ) {
            Object geometry = features.get(i).getGeometry();
            Object property = features.get(i).getProperties();

            log.info("property: " + property);
            String jsonGeometry = gson.toJson(geometry);
            String jsonProperty = gson.toJson(property);
            log.info("jsonGeometry: " + jsonGeometry); 
            log.info("jsonProperty: " + jsonProperty); 

            String SQL = "INSERT INTO " + checkedNameTable + " ( properties, geom ) VALUES ( '" + property + "', ST_Force3D(ST_SetSRID(ST_GeomFromGeoJSON('" + geometry + "'), 4326) ));";
            jdbcTemplate.batchUpdate(SQL);
        }

        return checkedNameTable;
    }

log.info("property: " + property) -> shows: property: {ELEMENTO=null} log.info("property: " + property) -> 显示: property: {ELEMENTO=null}

log.info("jsonGeometry: " + jsonGeometry) -> shows: jsonGeometry: {"type":"MultiPolygon","coordinates":[-3.691344883619331, 40.424004164131894] } log.info("jsonGeometry:" + jsonGeometry) -> 显示: jsonGeometry: {"type":"MultiPolygon","coordinates":[-3.691344883619331, 40.424004164131894] }

log.info("jsonProperty: " + jsonProperty) -> shows: jsonProperty: {} log.info("jsonProperty:" + jsonProperty) -> 显示:jsonProperty: {}

Here is a part of geojson:这是geojson的一部分:

{
    "type": "FeatureCollection",
    "features": [{
        "type": "Feature",
        "properties": {
            "ELEMENTO": null
        },
        "geometry": {
            "type": "MultiPolygon",
            "coordinates": [
                [
                    [
                        [-3.721150157449075, 40.41560855514652],
                        [-3.721148651840721, 40.41557793380572],
                        [-3.721118017962975, 40.41557902578145],
                        [-3.721113597363736, 40.41549617019551],
                        [-3.7211442312040024, 40.415495078221284],
                        [-3.721143913813499, 40.41546535037238],
                        [-3.72119576233073, 40.41546412605902],
                        [-3.721195464155001, 40.41543619989809],
                        [-3.7213085995073105, 40.415434593222656],
                        [-3.7213088881096974, 40.41546161853896],
                        [-3.721313602485271, 40.415461589126885],
                        [-3.7213206932891425, 40.41546334669616],
                        [-3.721326615119707, 40.415466012462055],
                        [-3.7213313679771427, 40.41546958642474],
                        [-3.7213372994291647, 40.41547315303393],
                        [-3.721340883314031, 40.41547763519334],
                        [-3.7213444671993554, 40.41548211735267],
                        [-3.721346882111449, 40.415487507709095],
                        [-3.7213975616676365, 40.415487191502386],
                        [-3.721400871367978, 40.41557636033541],
                        [-3.7213490227697066, 40.41557758474146],
                        [-3.7213478922770142, 40.415582096314125],
                        [-3.721345583188133, 40.415586615240166],
                        [-3.721343274098946, 40.415591134166164],
                        [-3.7213397767926453, 40.415594759601625],
                        [-3.7213351008897226, 40.415598392390244],
                        [-3.721330415365877, 40.415601124334835],
                        [-3.7213245416249427, 40.41560296278854],
                        [-3.7213186678836525, 40.41560480124199],
                        [-3.721316310690866, 40.415604815948214],
                        [-3.7213165896778255, 40.41563094041987],
                        [-3.7211999566810587, 40.41563617253719],
                        [-3.7211996585017926, 40.41560824637715],
                        [-3.721150157449075, 40.41560855514652]
                    ],
                    [
                        [-3.721260835104088, 40.41548714363045],
                        [-3.7212219511090683, 40.41548828710062],
                        [-3.7212160966125767, 40.415491927236594],
                        [-3.721188031570835, 40.41551282309202],
                        [-3.721190898542824, 40.41556055311452],
                        [-3.721222980337708, 40.41558467739598],
                        [-3.7212866245246525, 40.41558428036298],
                        [-3.7212912908093743, 40.41557974673244],
                        [-3.7213158296797295, 40.41555977375542],
                        [-3.72131649840387, 40.41551202167758],
                        [-3.721282040182673, 40.41548611043945],
                        [-3.721260835104088, 40.41548714363045]
                    ]
                ]
            ]
        }
    }]
}

By default Gson does not serialize null values.默认情况下, Gson不会序列化null值。 Since "ELEMENTO": null , it is excluded by default.由于"ELEMENTO": null ,默认情况下被排除在外。 That is the reason for the empty output.这就是空输出的原因。

To force the serialization of null values, use the following code:要强制序列化空值,请使用以下代码:

GsonBuilder builder = new GsonBuilder();
builder.serializeNulls();
Gson gson = builder.create();
String property = gson.toJson(property);

The GsonBuilder creates a Gson that serializes null values just as you want to have it. GsonBuilder创建了一个Gson ,它按照您想要的方式序列化空值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM