Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add json5 support to apoc-core and extended. #3586

Open
jexp opened this issue May 20, 2023 · 1 comment
Open

Add json5 support to apoc-core and extended. #3586

jexp opened this issue May 20, 2023 · 1 comment
Labels
core-functionality Adding new procedure, function or signature to APOC core extended-functionality waiting for feedback

Comments

@jexp
Copy link
Member

jexp commented May 20, 2023

Json5 adds a number of nice features.

In jackson we can enable most of them with capabilities. Some of which we already do.

See

https://stackoverflow.com/questions/68312227/can-the-jackson-parser-be-used-to-parse-json5

@vga91
Copy link
Contributor

vga91 commented Nov 18, 2024

Is not possible through apoc extended, we should have to handle the OBJECT_MAPPER that is in Core
(otherwise, we should rewrite all JsonUtil.loadJson in extended, that ofc is not an optimal solution).

Opened an analogous Trello Core Card with id: lD93dadu.

Therefore, we could do something like this in JsonUtil:

// import com.fasterxml.jackson.core.JsonParser;

// config could be something like a {enable: ['ALLOW_LEADING_DECIMAL_POINT_FOR_NUMBERS'], disable: ['ALLOW_UNQUOTED_FIELD_NAMES']}

// e.g. apoc.load.json(<file>, '', {enable: ['ALLOW_LEADING_DECIMAL_POINT_FOR_NUMBERS'], disable: ['ALLOW_UNQUOTED_FIELD_NAMES']})

public static ObjectMapper getJsonObjectMapper(Object result, Map<String, Object> config) throws JsonProcessingException {
    // create a copy of the JsonUtil.OBJECT_MAPPER
    ObjectMapper objectMapper = apoc.util.JsonUtil.OBJECT_MAPPER.copy();

    // enable and disable features
    List<String> enable = (List<String>) config.getOrDefault("enable", List.of());
    List<String> disable = (List<String>) config.getOrDefault("disable", List.of());
    enable.forEach(name -> objectMapper.enable(JsonParser.Feature.valueOf(name)));
    disable.forEach(name -> objectMapper.disable(JsonParser.Feature.valueOf(name)));

    return objectMapper;
}

And then, instead of the current writeValueAsString:

    public static String writeValueAsString(Object json) {
        return writeValueAsString(json, OBJECT_MAPPER);
    }

    public static String writeValueAsString(Object json, ObjectMapper objectMapper) {
        try {
            return objectMapper.writeValueAsString(json);
        } catch (JsonProcessingException e) {
            return null;
        }
    }

also, instead of the current Util.fromJson:

    public static <T> T fromJson(String value, Class<T> type) {
        return fromJson(value, type, JsonUtil.OBJECT_MAPPER);
    }    
    
    public static <T> T fromJson(String value, Class<T> type, ObjectMapper objectMapper) {
        try {
            return objectMapper.readValue(value, type);
        } catch (IOException e) {
            throw new RuntimeException("Can't convert " + value + " from JSON");
        }
    }

and so on.

@vga91 vga91 added extended-functionality core-functionality Adding new procedure, function or signature to APOC core labels Nov 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core-functionality Adding new procedure, function or signature to APOC core extended-functionality waiting for feedback
Projects
Status: Core issues
Development

No branches or pull requests

2 participants