Json to Dart built_value classes generator
run pub run build_runner serve --release
to check
link to project: https://charafau.github.io/json2builtvalue/
Json to Dart built_value classes generator
License: Apache License 2.0
Json to Dart built_value classes generator
run pub run build_runner serve --release
to check
link to project: https://charafau.github.io/json2builtvalue/
First of all thanks for the tool. It's awesome.
I found a problem when parsing keys with a parameter list. Could this be handled by some regex in generated key names perhaps?
input example:
{"Shape.STArea()": "Shape.STArea()"}
output:
Error: Could not format because the source could not be parsed:
line 14, column 68: Getters must be declared without a parameter list.
@BuiltValueField(wireName: 'Shape.STArea()') String get shapeSTArea();
^
I can work around it of course - it would just be nice ๐
I think swagger parser will be very useful.
The generator incorrectly parses double
values as int
when the fractional part of double
consists solely zeros.
For example, 1.0 or 1.00 both resolve to int
wheres 1.1 or 1.01 would resolve to double
. The latter case should always hold for either scenario.
The generated factories do not have the void
type on the return and then linter may warn about it
How it currently generates factories:
factory UserContext([Function(UserContextBuilder b) updates]) = _$UserContext;
It looks to me that this becomes important after dart 2, since the type inference is stronger.
This is related to:
The generated factory has the void already there
factory UserContext([void Function(UserContextBuilder b) updates]) = _$UserContext;
Thanks for the great tool :)
Just one question/observation--the BuiltValueField
annotation isn't needed, the wireName
by default is taken directly from the field name--would it make sense to omit it?
json2builtvalue/lib/parser.dart
Line 159 in 677fe77
After trying to parse some json strings with https://charafau.github.io/json2builtvalue/, it throws a RangeError exception when trying to parse empty lists and null values, e.g.:
{ "a": 1, "b": 2, "c": [], "d": null }
Wouldn't it be better if you return dynamic types in this cases?
I want to deserialize an json string:
{
"value" : 1.0
}
after transform by the tool from that string to the class, the property I receive is
@BuiltValueField(wireName: 'value')
int get value;
but the correct property must be
@BuiltValueField(wireName: 'value')
double get value;
in this case, the function will stop in this line:
case 'value':
result.value = serializers.deserialize(value,
specifiedType: const FullType(String)) as String;
break;
in myclass.g.dart file
{
"message_type": "GET_STORY",
"status": "Success",
"response_code": "1200",
"details": {
"content": [
{
"id": "1cbee0cc-11f2-11e9-8fc7-5130b7f9b02f",
"story": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla id justo vitae odio elementum elementum vel sit amet arcu. Maecenas posuere scelerisque risus, at cursus massa sodales quis. Vestibulum sit amet nisl vestibulum, laoreet arcu et, egestas justo. Fusce egestas viverra enim quis hendrerit. Nulla suscipit turpis et luctus tincidunt. Quisque ac bibendum odio, at iaculis est. Fusce eget ipsum sed augue varius rhoncus nec ac lorem. Nullam eu est bibendum, aliquet sapien id, congue sem. Duis pharetra eros consequat felis tempor cursus. Donec a accumsan lectus, sit amet condimentum est. Duis ac nisl nisl. Etiam dignissim lacinia ante, quis luctus sem accumsan non. Praesent risus turpis, viverra et mattis molestie, lobortis vulputate mi. Aliquam erat volutpat.\nCras luctus erat ligula, sed dignissim sapien consectetur non. Aenean ut dictum odio. Sed accumsan rutrum nisl id pulvinar. Vestibulum dolor ex, dignissim vel lacus ut, cursus bibendum lectus. Sed pretium lobortis lacinia. Aliquam tempor nunc et egestas imperdiet. Aliquam in nisl sed neque venenatis eleifend vel quis magna. Nullam in maximus est, sit amet consequat magna. In sed rhoncus eros, nec ornare diam. Mauris vel mi non nisi ultricies consectetur.\r\nSuspendisse nibh elit, fringilla id massa in, eleifend tristique mauris. Nunc euismod nibh sit amet quam tincidunt, a ultrices mauris laoreet. Donec ut cursus augue. Quisque eu molestie erat. Nullam eget consequat sapien. Donec vitae justo et lacus posuere aliquet. Sed condimentum blandit nisi nec ullamcorper. Nulla lacinia varius nisl, sed mollis nulla tempor at. Quisque ex sem, consectetur id nulla ut, tristique tincidunt turpis. Suspendisse quis lacinia elit.\r\nMauris eget eleifend lorem. Donec laoreet sem lorem, in fringilla sem maximus a. Sed libero nisl, consectetur fringilla eros vitae, interdum auctor augue. Mauris aliquam turpis lorem, id fermentum dui porta nec. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus erat massa, imperdiet sed auctor a, tristique nec felis. Fusce eu eros vitae ipsum vulputate viverra et at nulla. Phasellus consectetur est lectus, in placerat sapien vestibulum at. Fusce ex arcu, condimentum ut nisi eget, venenatis maximus justo. Nulla ac ullamcorper massa, quis pharetra libero. Suspendisse dolor magna, congue id est in, egestas tristique nisl. Aliquam sed augue vitae neque hendrerit mollis et sed neque. Suspendisse aliquam lorem vel iaculis facilisis.\r\nFusce gravida lorem commodo elit tincidunt condimentum in et urna. Vivamus suscipit ac nibh non ornare. Donec ultricies nibh sit amet laoreet vestibulum. Phasellus id rutrum nibh, id maximus erat. Ut tincidunt dignissim viverra. Duis vitae velit nulla. Nam id nibh in augue rutrum consequat ac et enim. Pellentesque ut odio tempus, pharetra elit suscipit, posuere elit. Vivamus eu lacus lobortis, consectetur orci vitae, rhoncus nisi. Nullam massa orci, feugiat sit amet imperdiet vitae, pretium id orci. Pellentesque malesuada laoreet mauris, et rhoncus sem facilisis at. Sed a pretium leo, vel ornare massa. Praesent id eros eu metus egestas dignissim. Morbi varius augue a pellentesque rhoncus. Ut et ligula vestibulum, iaculis ante id, dictum lorem. ",
"category": "lorem_ipsum",
"thumbnailPath": "some_path_to_thumbnail",
"heading": "LOREM IPSUM LOREM IPSUM LOREM IPSUM",
"annonymous": true,
"createdAt": "2019-01-06T20:31:57.000+0000",
"no_of_likes": 0,
"no_of_comments": 0
}
],
"pageable": {
"sort": {
"sorted": true,
"unsorted": false
},
"pageSize": 20,
"pageNumber": 0,
"offset": 0,
"unpaged": false,
"paged": true
},
"last": true,
"totalPages": 1,
"totalElements": 5,
"first": true,
"sort": {
"sorted": true,
"unsorted": false
},
"numberOfElements": 5,
"size": 20,
"number": 0
}
}
output:
import 'dart:convert';
import 'package:built_collection/built_collection.dart';
import 'package:built_value/built_value.dart';
import 'package:built_value/serializer.dart';
part 'stories.g.dart';
abstract class Stories implements Built<Stories, StoriesBuilder> {
Stories._();
factory Stories([updates(StoriesBuilder b)]) = _$Stories;
@BuiltValueField(wireName: 'message_type')
String get messageType;
@BuiltValueField(wireName: 'status')
String get status;
@BuiltValueField(wireName: 'response_code')
String get responseCode;
@BuiltValueField(wireName: 'details')
Details get details;
String toJson() {
return json.encode(serializers.serializeWith(Stories.serializer, this));
}
static Stories fromJson(String jsonString) {
return serializers.deserializeWith(
Stories.serializer, json.decode(jsonString));
}
static Serializer<Stories> get serializer => _$storiesSerializer;
}
library details;
import 'dart:convert';
import 'package:built_collection/built_collection.dart';
import 'package:built_value/built_value.dart';
import 'package:built_value/serializer.dart';
part 'details.g.dart';
abstract class Details implements Built<Details, DetailsBuilder> {
Details._();
factory Details([updates(DetailsBuilder b)]) = _$Details;
@BuiltValueField(wireName: 'content')
BuiltList<Content> get content;
@BuiltValueField(wireName: 'pageable')
Pageable get pageable;
@BuiltValueField(wireName: 'last')
bool get last;
@BuiltValueField(wireName: 'totalPages')
int get totalPages;
@BuiltValueField(wireName: 'totalElements')
int get totalElements;
@BuiltValueField(wireName: 'first')
bool get first;
@BuiltValueField(wireName: 'sort')
Sort get sort;
@BuiltValueField(wireName: 'numberOfElements')
int get numberOfElements;
@BuiltValueField(wireName: 'size')
int get size;
@BuiltValueField(wireName: 'number')
int get number;
String toJson() {
return json.encode(serializers.serializeWith(Details.serializer, this));
}
static Details fromJson(String jsonString) {
return serializers.deserializeWith(
Details.serializer, json.decode(jsonString));
}
static Serializer<Details> get serializer => _$detailsSerializer;
}
the whole content, sort and paggeable class missing
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.