簡體   English   中英

為什么在 gson.toJSon 時返回一個空對象

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

我正在嘗試使用文件流解析 geojson 文件,因為該文件可能是一個巨大的文件。 1KB、20MG 或 1TB。

正如您在 ParseGeojsonStream 類中看到的那樣,我解析了所有文件使用流。

但是當我嘗試使用 gson 解析為 json 以將其存儲在 postgressql 數據庫中時,字段屬性是一個空對象。

但是我在轉換為 json 之前做了一個日志,因為它不是空的。

我希望你能理解我。

通量是下一個:

  • 首先,我從我的控制器接收到一個 post 字符串屬性和一個 inputStream。
  • 接下來,我的服務調用 ParseGeojosnStream 來流式傳輸文件的內容。
  • 接下來,我傳遞名稱和特征列表並插入到表中。
  • 最后,我使用字符串屬性和表名作為 Id 創建方案。

那么,為什么在轉換為 json 以將其存儲在數據庫中時返回一個空的屬性對象?

這是代碼:

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;
    }

}

這存儲了所有特征,這里是一個條目的日志: Feature: Feature(properties={ELEMENTO=null}, geometry={type=MultiPolygon, coordinates=[-3.691344883619331, 40.424004164131894] })

特征類:

@Data 
public class Feature {

    private Object properties;
    private Object geometry;
}

來自 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) -> 顯示: property: {ELEMENTO=null}

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

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

這是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]
                    ]
                ]
            ]
        }
    }]
}

默認情況下, Gson不會序列化null值。 由於"ELEMENTO": null ,默認情況下被排除在外。 這就是空輸出的原因。

要強制序列化空值,請使用以下代碼:

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

GsonBuilder創建了一個Gson ,它按照您想要的方式序列化空值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM