|
| 1 | +// Copyright (c) 2017, Google Inc. Please see the AUTHORS file for details. |
| 2 | +// All rights reserved. Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +import 'package:collection/collection.dart'; |
| 6 | + |
| 7 | +/// A JSON value. |
| 8 | +/// |
| 9 | +/// This class is suitable for use in built_value fields. When serialized it |
| 10 | +/// maps directly onto JSON values. |
| 11 | +/// |
| 12 | +/// Deep operator== and hashCode are provided, meaning the contents of a |
| 13 | +/// List or Map is used for equality and hashing. |
| 14 | +/// |
| 15 | +/// List and Map classes are wrapped in [UnmodifiableListView] and |
| 16 | +/// [UnmodifiableMapView] so they won't be modifiable via this object. You |
| 17 | +/// must ensure that no updates are made via the original reference, as a |
| 18 | +/// copy is not made. |
| 19 | +abstract class JsonObject { |
| 20 | + /// The value, which may be a bool, a List, a Map, a num or a String. |
| 21 | + Object get value; |
| 22 | + |
| 23 | + /// Whether the value is a [bool]. |
| 24 | + bool get isBool => false; |
| 25 | + |
| 26 | + /// The value as a [bool], or throw if not. |
| 27 | + bool get asBool => throw new StateError('Not a bool.'); |
| 28 | + |
| 29 | + /// Whether the value is a [List]. |
| 30 | + bool get isList => false; |
| 31 | + |
| 32 | + /// The value as a [List], or throw if not. |
| 33 | + List get asList => throw new StateError('Not a List.'); |
| 34 | + |
| 35 | + /// Whether the value is a [Map]. |
| 36 | + bool get isMap => false; |
| 37 | + |
| 38 | + /// The value as a [Map], or throw if not. |
| 39 | + Map get asMap => throw new StateError('Not a Map.'); |
| 40 | + |
| 41 | + /// Whether the value is a [num]. |
| 42 | + bool get isNum => false; |
| 43 | + |
| 44 | + /// The value as a [num], or throw if not. |
| 45 | + num get asNum => throw new StateError('Not a num.'); |
| 46 | + |
| 47 | + /// Whether the value is a [String]. |
| 48 | + bool get isString => false; |
| 49 | + |
| 50 | + /// The value as a [String], or throw if not. |
| 51 | + String get asString => throw new StateError('Not a String.'); |
| 52 | + |
| 53 | + /// Instantiates with [value], which must be a bool, a List, a Map, a num |
| 54 | + /// or a String. Otherwise, an [ArgumentError] is thrown. |
| 55 | + factory JsonObject(Object value) { |
| 56 | + if (value is num) { |
| 57 | + return new NumJsonObject(value); |
| 58 | + } else if (value is String) { |
| 59 | + return new StringJsonObject(value); |
| 60 | + } else if (value is bool) { |
| 61 | + return new BoolJsonObject(value); |
| 62 | + } else if (value is List<Object>) { |
| 63 | + return new ListJsonObject(value); |
| 64 | + } else if (value is Map<String, Object>) { |
| 65 | + return new MapJsonObject(value); |
| 66 | + } else { |
| 67 | + throw new ArgumentError.value(value, 'value', |
| 68 | + 'Must be bool, List<Object>, Map<String, Object>, num or String.'); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + JsonObject._(); |
| 73 | + |
| 74 | + @override |
| 75 | + String toString() { |
| 76 | + return value.toString(); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +/// A [JsonObject] holding a bool. |
| 81 | +class BoolJsonObject extends JsonObject { |
| 82 | + @override |
| 83 | + final bool value; |
| 84 | + |
| 85 | + BoolJsonObject(this.value) : super._(); |
| 86 | + |
| 87 | + @override |
| 88 | + bool get isBool => true; |
| 89 | + |
| 90 | + @override |
| 91 | + bool get asBool => value; |
| 92 | + |
| 93 | + @override |
| 94 | + bool operator ==(dynamic other) { |
| 95 | + if (identical(other, this)) return true; |
| 96 | + if (other is! BoolJsonObject) return false; |
| 97 | + return value == other.value; |
| 98 | + } |
| 99 | + |
| 100 | + @override |
| 101 | + int get hashCode => value.hashCode; |
| 102 | +} |
| 103 | + |
| 104 | +/// A [JsonObject] holding a List. |
| 105 | +class ListJsonObject extends JsonObject { |
| 106 | + @override |
| 107 | + final List<Object> value; |
| 108 | + |
| 109 | + ListJsonObject(List<Object> value) |
| 110 | + : this.value = new UnmodifiableListView<Object>(value), |
| 111 | + super._(); |
| 112 | + |
| 113 | + @override |
| 114 | + bool get isList => true; |
| 115 | + |
| 116 | + @override |
| 117 | + List<Object> get asList => value; |
| 118 | + |
| 119 | + @override |
| 120 | + bool operator ==(dynamic other) { |
| 121 | + if (identical(other, this)) return true; |
| 122 | + if (other is! ListJsonObject) return false; |
| 123 | + return const DeepCollectionEquality().equals(value, other.value); |
| 124 | + } |
| 125 | + |
| 126 | + @override |
| 127 | + int get hashCode => const DeepCollectionEquality().hash(value); |
| 128 | +} |
| 129 | + |
| 130 | +/// A [JsonObject] holding a Map. |
| 131 | +class MapJsonObject extends JsonObject { |
| 132 | + @override |
| 133 | + final Map<String, Object> value; |
| 134 | + |
| 135 | + MapJsonObject(Map<String, Object> value) |
| 136 | + : this.value = new UnmodifiableMapView(value), |
| 137 | + super._(); |
| 138 | + |
| 139 | + @override |
| 140 | + bool get isMap => true; |
| 141 | + |
| 142 | + @override |
| 143 | + Map<String, Object> get asMap => value; |
| 144 | + |
| 145 | + @override |
| 146 | + bool operator ==(dynamic other) { |
| 147 | + if (identical(other, this)) return true; |
| 148 | + if (other is! MapJsonObject) return false; |
| 149 | + return const DeepCollectionEquality().equals(value, other.value); |
| 150 | + } |
| 151 | + |
| 152 | + @override |
| 153 | + int get hashCode => const DeepCollectionEquality().hash(value); |
| 154 | +} |
| 155 | + |
| 156 | +/// A [JsonObject] holding a num. |
| 157 | +class NumJsonObject extends JsonObject { |
| 158 | + @override |
| 159 | + final num value; |
| 160 | + |
| 161 | + NumJsonObject(this.value) : super._(); |
| 162 | + |
| 163 | + @override |
| 164 | + bool get isNum => true; |
| 165 | + |
| 166 | + @override |
| 167 | + num get asNum => value; |
| 168 | + |
| 169 | + @override |
| 170 | + bool operator ==(dynamic other) { |
| 171 | + if (identical(other, this)) return true; |
| 172 | + if (other is! NumJsonObject) return false; |
| 173 | + return value == other.value; |
| 174 | + } |
| 175 | + |
| 176 | + @override |
| 177 | + int get hashCode => value.hashCode; |
| 178 | +} |
| 179 | + |
| 180 | +/// A [JsonObject] holding a String. |
| 181 | +class StringJsonObject extends JsonObject { |
| 182 | + @override |
| 183 | + final String value; |
| 184 | + |
| 185 | + StringJsonObject(this.value) : super._(); |
| 186 | + |
| 187 | + @override |
| 188 | + bool get isString => true; |
| 189 | + |
| 190 | + @override |
| 191 | + String get asString => value; |
| 192 | + |
| 193 | + @override |
| 194 | + bool operator ==(dynamic other) { |
| 195 | + if (identical(other, this)) return true; |
| 196 | + if (other is! StringJsonObject) return false; |
| 197 | + return value == other.value; |
| 198 | + } |
| 199 | + |
| 200 | + @override |
| 201 | + int get hashCode => value.hashCode; |
| 202 | +} |
0 commit comments