Giter VIP home page Giter VIP logo

Comments (15)

snuyanzin avatar snuyanzin commented on June 24, 2024 1

I'm ok to use jackson however in this case we probably should shade it as well (in order to prevent clashing of deps in case someone uses datafaker from on side and from another side has dependency on a different jackson version)

Probably we could still keep JsonTransformerBuilder NON deprecated and use it it pass ObjectMapper flags in case of non default behavior is required, e.g. order of map objects by keys (suddenly i faced thatdefault behavior for this sometimes changes between jdk17 and jdk21)

from datafaker.

snuyanzin avatar snuyanzin commented on June 24, 2024

As a WA it could be handled with help of this

JsonTransformer<Object> transformer = JsonTransformer.builder().formattedAs(FormattedAs.JSON_ARRAY).build();

after that there will be square brackets

from datafaker.

snuyanzin avatar snuyanzin commented on June 24, 2024

I'll be more than happy to contribute to this fix if you guys agree.

there is already some logic for handling this, may be need to tweak in a better way when FormattedAs.JSON_ARRAY and when FormattedAs.JSON_OBJECT should be used...

feel free to contribute here

from datafaker.

eliasnogueira avatar eliasnogueira commented on June 24, 2024

@snuyanzin Thank you for the explanation.

Wouldn't be better to remove the Formatter and add the logic during the generation?
I'm suggesting it because more than 2 objects will be wrongly generated if we don't use the formattedAs(). I can't see any use case as the consumer would generate the objects starting the JSON file with a curly brace.

from datafaker.

eliasnogueira avatar eliasnogueira commented on June 24, 2024

Btw, I will add an issue to update the docs with the mention of the formattedAs() method.

from datafaker.

snuyanzin avatar snuyanzin commented on June 24, 2024

Wouldn't be better to remove the Formatter and add the logic during the generation?

I remember the was some questionable cases, if we can double check that everything is remained covered then probably yes could be removed

from datafaker.

eliasnogueira avatar eliasnogueira commented on June 24, 2024

@snuyanzin @bodiam just adding the last comment before I start changing it as a new PR.

Proposed change

Deprecation of the JsonTransformerBuilder in favor of the automatic recognition of JSON object vs JSON Array.

As an example, Jackson, widely used by Java developers to se/deserializartion does it automatically based on the input object. In the datafaker case, it will be based on the limit value.

Example: the following code, using the jackson-dataformat-xml library shows that the library will generate an object or an array based on the input for the writeValueAsString() method.

public class JsonSerialization {

    private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();


    public static void main(String[] args) throws JsonProcessingException {
        /*
         * output
         * {"name":"Elias","email":"[email protected]"}
         */
        generateAsObject();
        
        /* 
         * output
         * [{"name":"Sergey","email":"[email protected]"},{"name":"Erik","email":"[email protected]"}]
         */
        generateAsArray();
    }

    private static void generateAsObject() throws JsonProcessingException {
        var person = new Person("Elias", "[email protected]");
        String output = OBJECT_MAPPER.writeValueAsString(person);

        System.out.println(output);
    }

    private static void generateAsArray() throws JsonProcessingException {
        var list = List.of(new Person("Sergey", "[email protected]"), new Person("Erik", "[email protected]"));

        String output = OBJECT_MAPPER.writeValueAsString(list);

        System.out.println(output);
    }

    record Person(String name, String email) {}
}

What do you guys think?

from datafaker.

eliasnogueira avatar eliasnogueira commented on June 24, 2024

What I meant was not using Jackson. I used it to illustrate its behavior for objects or arrays.

from datafaker.

snuyanzin avatar snuyanzin commented on June 24, 2024

ok then the question: how inside generateJson or whatever method which will detect JSON Object or JSON Array we could answer a question "should we generate an object or an array with length 1?"

Ok seems the question above is already solved in the code

At the same time there is another method JsonTransformer.JsonTransformerBuilder#withCommaBetweenObjects which is a part of JsonTransformerBuilder and since there is no alternative for that I don't think we can say that JsonTransformerBuilder could be marked as deprecated

The idea of withCommaBetweenObjects that if we set it to false it could produce output in JSON Lines way (https://jsonlines.org/examples/) which would make sense while generation of data for BigQuery for instance

from datafaker.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.