NaN
NaN

Reputation: 3591

object de/serialization using dart

I have to serialize some WebRTC-related dart objects to send them over a signaling channel. As example I have to encode RtcSessionDescription and RtcIceCandidate instances. Both classes offer a constructor to build them in context of a given map, but no one offers a method to create such a Map out of the original object.

How can I generate strings? Do I have to make a detour over Map-objects?

As Example:

RtcSessionDescription -> Map -> String -(send_over_signalingChannel)-> String -> Map -> RtcSessionDescription

Upvotes: 1

Views: 363

Answers (2)

NaN
NaN

Reputation: 3591

Finally I found a solution (using dart:convert as Günther Zöchbauer suggested):

RtcSessionDescription original = ...;

//serialize
final String serialized_sdp = JSON.encode({
    'sdp':original.sdp,
    'type':original.type
});


//decode
final Map sdp_map = JSON.decode(serialized_sdp);    
RtcSessionDescription sdp = new RtcSessionDescription(sdp_map);

Upvotes: 1

Günter Zöchbauer
Günter Zöchbauer

Reputation: 657781

You can easily convert between Map and String using the dart:convert package.

https://www.dartlang.org/articles/json-web-service/

I don't know about RtcSessionDescription <-> Map though.

See also this question: Can I automatically serialize a Dart object to send over a Web Socket?

Upvotes: 1

Related Questions