Home > Net >  Dart/Flutter : convert BigInt to json
Dart/Flutter : convert BigInt to json

Time:08-02

I have an object with a bigint property i want to encode to json string:

class Token {
  Token(
      {
      this.name,
      this.supply,
      });

  String? name;
  BigInt? supply;

  factory Token.fromJson(Map<String, dynamic> json) {
    return Token(
      name: json['name'],
      supply: json['supply'] == null ? null : BigInt.parse(json['supply']),
    );
  }

  Map<String, dynamic> toJson() => <String, dynamic>{
     'name': name,
     'supply': supply == null ? null : supply!.toString(),
  };
}

I create a method to encode json to string...

String tokenToJson(Token data) => jsonEncode(data.toJson())

... but the format is not correct because i need a bigint in the format json and not a string:

the result i want: {"name":"Token","supply":100000000000000,}

the result i obtain: {"name":"Token","supply":"100000000000000",}

jsonEncode doesn't manage bigint type and i found on internet only solutions with a conversion of the bigint to a string type.

NB: Same issue with jsonDecode

Thx

CodePudding user response:

use this simple method instead of BigInt use just "num"

        import 'dart:convert';
                void main() {
                String data = '''{"name":"Token","supply":100000000000000}''';
                print("supply: ${Token.fromJson(jsonDecode(data)).supply}");
                }
        
        
        class Token {
            Token({
                required this.name,
                        required this.supply,
            });
            late final String name;
            late final num supply;
          
          Token.fromJson(Map<String, dynamic> json){
                name = json['name'];
                supply = json['supply'];
            }
        
            Map<String, dynamic> toJson() {
                final _data = <String, dynamic>{};
                _data['name'] = name;
                _data['supply'] = supply;
                return _data;
            }
        }

CodePudding user response:

Please check the following answer, instead of calling toString method on supply just call toInt method which will prevents the quotations and you will get the formatted json as expected

import 'dart:convert';

class Token {
  Token(
      {
      this.name,
      this.supply,
      });

  String? name;
  BigInt? supply;

  factory Token.fromJson(Map<String, dynamic> json) {
    return Token(
      name: json['name'],
      supply: json['supply'] == null ? null :BigInt.from(json['supply']) ,
    );
  }

  Map<String, dynamic> toJson() => <String, dynamic>{
     'name': name,
     'supply': supply == null ? null : supply!.toInt(),
  };
  
    String tokenToJson(Token data) => json.encode(data.toJson());
}

void main() {
  
  Token token = Token(name: "token_one",supply : BigInt.parse("10000061234567"));
  print(token.tokenToJson(token));

}

Output

{"name":"token_one","supply":10000061234567}

CodePudding user response:

You don't need to parse, you can use from for convert int to BigInt

import 'dart:convert';
void main() {
  String data = '''{"name":"Token","supply":100000000000000}''';
  print(Token.fromJson(jsonDecode(data)).toJson());
}


class Token {
  String? name;
  double? supply;

  Token({this.name, this.supply});

  Token.fromJson(Map<String, dynamic> json) {
    name = json['name'];
    supply = json['supply'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['name'] = name;
    data['supply'] = supply;
    return data;
  }
}

  • Related