Comments (15)
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.
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.
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.
@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.
Btw, I will add an issue to update the docs with the mention of the formattedAs()
method.
from datafaker.
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.
@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.
What I meant was not using Jackson. I used it to illustrate its behavior for objects or arrays.
from datafaker.
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)
- Add constrained fields feature HOT 2
- Add Muslim Names Feature Request HOT 3
- Lorem words are not unique HOT 9
- Fake schema generating always the same name HOT 3
- Diffierent value if recall values HOT 2
- aws().vpcId() generates invalid id HOT 12
- Missing providers in the docs and old link references
- The faker.date().birthday() method returns different dates when using seed HOT 5
- StackOverflowError for regexify
- Cannot use datafaker in a project that use module (module-info.java) HOT 24
- Invalid date 'February 29' as '1925' is not a leap year HOT 13
- 'new Faker().locality().localeString()' throws java.lang.NullPointerException HOT 4
- Couldn't generate date at leap year HOT 3
- birthday throwing exception if run at 2/29/2024 HOT 7
- Faker.date().birthday() returns in invalid Date on Leap Day HOT 2
- Duplicate faker groups (Money and Currency) HOT 9
- Release 2.2.0 HOT 10
- Add Java 22 to the build matrix. HOT 6
- Adding a Sensor data provider for testing IoT applications HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from datafaker.