1
import 'package:json_string/src/functions.dart';
2
import 'package:json_string/src/jsonable.dart';
3
import 'package:json_util/json_util.dart';
4

5
/// Makes an object ready to be encoded.
6 1
Map<String, dynamic> disassembleObject<T extends Object>(T value,
7
    {JsonObjectEncoder<T> builder}) {
8
  final dartObject = value;
9
  if (builder != null) {
10 1
    return builder(dartObject);
11 1
  } else if (dartObject is Jsonable) {
12 1
    return dartObject.toMap();
13
  }
14 1
  throw JsonEncodingError(
15
      'this is not a Jsonable object: provide a valid encoder.');
16
}
17

18
/// Makes a list of objects ready to be encoded.
19 1
List<Map<String, dynamic>> disassembleObjectList<T extends Object>(
20
    List<T> value,
21
    {JsonObjectEncoder<T> builder}) {
22
  final dartObjectList = value;
23 1
  return dartObjectList.map((e) {
24
    if (e == null) {
25
      return e as Map<String, dynamic>;
26
    }
27 1
    return disassembleObject<T>(e, builder: builder);
28 1
  }).toList();
29
}
30

31
/// Wrap a generic operation from json_util library
32 1
T wrapJsonUtilOperation<T>(T Function() exec) {
33
  try {
34 1
    return exec();
35 1
  } on DecodedValueError catch (e) {
36 1
    throw JsonDecodingError(e);
37 1
  } on EncodableValueError catch (e) {
38 1
    throw JsonEncodingError(e);
39
  }
40
}

Read our documentation on viewing source code .

Loading