티스토리 뷰

[Flutter] JsonSerialize(encode / decode)

 

클래스 맴버로 toJson, fromJson 메서드를 만들어 준 후

dart:convert 라이브러리의 jsonEncode, jsonDecode 함수로 변환해준다.

 

https://api.flutter.dev/flutter/dart-convert/jsonEncode.html

 

jsonEncode function - dart:convert library - Dart API

jsonEncode function String jsonEncode(Object? object, {Object? toEncodable(Object? nonEncodable )?} ) Converts object to a JSON string. If value contains objects that are not directly encodable to a JSON string (a value that is not a number, boolean, strin

api.flutter.dev

 

json_serializable 패키지를 사용하는 방법도 있는데 다른 언어(C#, Java, javascript)들 처럼 간편하진 않다.

@JsonSerializable 어노테이션을 선언 한 후 json_serializable 패키지 명령어로 utility code( _$클래스명ToJson, _$클래스명FromJson )를 생성해서 위와 같은 방식으로 toJson, fromJson에 생성된 utiltiy code의 함수를 반환해주는 방식이다.

 

https://docs.flutter.dev/data-and-backend/serialization/json

 

JSON and serialization

How to use JSON with Flutter.

docs.flutter.dev

 

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/06   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30
글 보관함