Docs Menu
Docs Home
/ /
Atlas Device SDK
/ /

직렬화 - Flutter SDK

이 페이지의 내용

  • 직렬화 에 지원되는 데이터 유형
  • Realm 객체 직렬화
  • EJSON으로 직렬화
  • EJSON에서 역직렬화
  • 비 Realm 객체 직렬화
  • 사용자 지정 코덱 등록

Flutter용 Atlas Device SDK 는 정적 Realm 객체와의 확장 JSON (EJSON) 직렬화 및 역직렬화를 지원합니다.

Flutter SDK 는 현재 다음과 같은 지원되는 데이터 유형의 직렬화를 지원합니다.

  • 모든 다트 언어 데이터 유형

  • 십진수128 및 RealmValue를 제외한 모든 Realm별 데이터 유형

다음 표는 SDK의 Realm 관련 데이터 유형이 출력 예시를 통해 직렬화되는 방법을 보여줍니다.

Realm 유형
다음으로 직렬화
날짜/시간

날짜

DateTime birthDate = DateTime.utc(2024, 4, 10) 직렬화 birthDate: {$date: {$numberLong: 1712707200000}}

RealmList

배열

List<String> listOfStrings = [food, water] 직렬화 listOfStrings: [food, water]

RealmMap

배열

Map<String, int> mapOfMixedAnyValues = {'first': 123 , 'second': 567} 직렬화 mapOfValues: {first: {$numberInt: 123}, second: {$numberInt: 567}}

RealmSet

배열

Set<int> setOfInts = {0, 1, 2, 3} 직렬화 setOfInts: [{$numberInt: 0}, {$numberInt: 1}, {$numberInt: 2}, {$numberInt: 3}]

ObjectId

ObjectId

ObjectId id = ObjectId() 직렬화 {id: {$oid: 666a6fd54978af08e54a8d52}

UUID

바이너리

Uuid myId = Uuid.v4() 직렬화 myId: {$binary: {base64: 6TvsMWxDRWa1jSC6gxiM3A==, subType: 04}}

Uint8List

바이너리

Uint8List aBinaryProperty = Uint8List.fromList([1, 2]) 직렬화 aBinaryProperty: {$binary: {base64: AQI=, subType: 00}}

객체

문서

Address address = Address("500 Dean Street", "Brooklyn", "NY", "USA") 직렬화 address: {street: 500 Dean Street, city: Brooklyn, state: NY, country: USA}

Realm이 아닌 유형의 직렬화에 대한 자세한 내용은 BSON 데이터 유형 및 관련 표현을 참조하세요.

Flutter SDK 직렬화는 현재 코드, CodeWScope, DBPointer, DBRef, 정규 표현식, 타임스탬프와 같은 BSON types를 지원 하지 않습니다.

SDK의 전체 문서 인코더를 사용하면 사용자 정의 클래스를 직렬화 및 역직렬화할 수 있습니다.

인코더를 사용하려면 일반적으로 @RealmModel() 주석을 사용하는 것처럼 객체 모델 을 만듭니다. part 선언으로 생성된 RealmObject 클래스 모델은 직렬화 및 역직렬화에 필요한 메서드를 제공합니다.

이 페이지의 예제에서는 다음 Pet 객체 모델 이 사용됩니다.

import 'package:realm_dart/realm.dart';
part 'pet.realm.dart';
@RealmModel()
class _Pet {
late String type;
late int numberOfLegs;
late DateTime birthDate;
late double? price;
}

클래스 기반 객체의 경우 RealmObject toEjson() 을 사용하여 EJSON으로 직렬화할 수 있습니다. 다음 두 가지 방법으로 메서드를 사용할 수 있습니다.

// Pass the object as a parameter to the method
EJsonValue serializeByParam = toEJson(spider);
// Call the method directly on the object
EJsonValue serializeWithCall = spider.toEJson();
{
type: Jumping Spider,
numberOfLegs: {$numberInt: 8},
birthDate: {$date: {$numberLong: 1712707200000}},
price: null
}

fromEjson() 을 사용하여 EJSON에서 역직렬화 메서드. 이 메서드는 지정된 객체 유형에 대한 EJSON을 입력으로 사용하고 지정된 객체 유형의 역직렬화된 인스턴스 를 출력합니다.

다음 예시 에서는 이전 예시 에서 serializeByParam 을 역직렬화합니다.

// Pass the serialized object to the method
final deserializeFromEjsonWithExplicitType = fromEJson<Pet>(serializeByParam);
// The method can also infer the object type
Pet deserializeFromEjson = fromEJson(serializeByParam);

Realm이 아닌 클래스의 경우 클래스 생성자에서 @ejson 주석을 사용하여 디코더와 인코더를 생성할 수 있습니다.

class Person {
final String name;
final DateTime birthDate;
final int? age;
final double income;
final Person? spouse;
@ejson // annotate constructor to generate decoder and encoder
Person(this.name, this.birthDate, this.income, {this.spouse, this.age});
}

SDK는 사용자 지정 EJSON 코덱도 지원합니다. 앱 에서 사용하려면 등록 하세요. 지정된 유형에 대한 사용자지정 EJSON 인코더 및 디코더입니다.

돌아가기

데이터 동결