Skip to content

JString final

final class JString extends Json

A JSON string value.

Inheritance

Object → JsonJString

Constructors

JString() const

const JString(String value)
Implementation
dart
const JString(this.value);

Properties

hashCode no setter inherited

int get hashCode

The hash code for this object.

A hash code is a single integer which represents the state of the object that affects operator == comparisons.

All objects have hash codes. The default hash code implemented by Object represents only the identity of the object, the same way as the default operator == implementation only considers objects equal if they are identical (see identityHashCode).

If operator == is overridden to use the object state instead, the hash code must also be changed to represent that state, otherwise the object cannot be used in hash based data structures like the default Set and Map implementations.

Hash codes must be the same for objects that are equal to each other according to operator ==. The hash code of an object should only change if the object changes in a way that affects equality. There are no further requirements for the hash codes. They need not be consistent between executions of the same program and there are no distribution guarantees.

Objects that are not equal are allowed to have the same hash code. It is even technically allowed that all instances have the same hash code, but if clashes happen too often, it may reduce the efficiency of hash-based data structures like HashSet or HashMap.

If a subclass overrides hashCode, it should override the operator == operator as well to maintain consistency.

Inherited from Json.

Implementation
dart
@override
int get hashCode => fold(
  () => 0,
  (boolean) => boolean.hashCode,
  (number) => number.hashCode,
  (string) => string.hashCode,
  (ilist) => ilist.hashCode,
  (object) => object.keys.hashCode * object.values.hashCode,
);

hcursor no setter inherited

HCursor get hcursor

Returns an HCursor positioned at the root of this value.

Inherited from Json.

Implementation
dart
HCursor get hcursor => HCursor.fromJson(this);

isArray no setter override

bool get isArray

Returns true if this value is a JArray.

Implementation
dart
@override
bool get isArray => false;

isBoolean no setter override

bool get isBoolean

Returns true if this value is a JBoolean.

Implementation
dart
@override
bool get isBoolean => false;

isNull no setter override

bool get isNull

Returns true if this value is JNull.

Implementation
dart
@override
bool get isNull => false;

isNumber no setter override

bool get isNumber

Returns true if this value is a JNumber.

Implementation
dart
@override
bool get isNumber => false;

isObject no setter override

bool get isObject

Returns true if this value is a JObject.

Implementation
dart
@override
bool get isObject => false;

isString no setter override

bool get isString

Returns true if this value is a JString.

Implementation
dart
@override
bool get isString => true;

runtimeType no setter inherited

Type get runtimeType

A representation of the runtime type of the object.

Inherited from Object.

Implementation
dart
external Type get runtimeType;

value final

final String value

The string value.

Implementation
dart
final String value;

Methods

asArray() override

Option<IList<Json>> asArray()

Returns Some with the elements if this is JArray, otherwise None.

Implementation
dart
@override
Option<IList<Json>> asArray() => none();

asBoolean() override

Option<bool> asBoolean()

Returns Some with the boolean if this is JBoolean, otherwise None.

Implementation
dart
@override
Option<bool> asBoolean() => none();

asNull() override

Option<Unit> asNull()

Returns Some with Unit if this is JNull, otherwise None.

Implementation
dart
@override
Option<Unit> asNull() => none();

asNumber() override

Option<num> asNumber()

Returns Some with the number if this is JNumber, otherwise None.

Implementation
dart
@override
Option<num> asNumber() => none();

asObject() override

Option<JsonObject> asObject()

Returns Some with the JsonObject if this is JObject, otherwise None.

Implementation
dart
@override
Option<JsonObject> asObject() => none();

asString() override

Option<String> asString()

Returns Some with the string if this is JString, otherwise None.

Implementation
dart
@override
Option<String> asString() => Some(value);

deepDropNullValues() inherited

Json deepDropNullValues()

Returns a copy with all JNull values removed recursively.

Inherited from Json.

Implementation
dart
Json deepDropNullValues() => foldWith(_DropNullFolder());

deepMerge() inherited

Json deepMerge(Json that)

Recursively merges that into this value.

Object fields from this value take precedence; fields present in both are merged recursively. If either value is not a JObject, returns that.

Inherited from Json.

Implementation
dart
Json deepMerge(Json that) => (asObject(), that.asObject())
    .mapN(
      (lhs, rhs) => fromJsonObject(
        lhs.toIList().foldLeft(
          rhs,
          (acc, kv) => rhs
              .get(kv.$1)
              .fold(
                () => acc.add(kv.$1, kv.$2),
                (r) => acc.add(kv.$1, kv.$2.deepMerge(r)),
              ),
        ),
      ),
    )
    .getOrElse(() => that);

dropNullValues() inherited

Json dropNullValues()

Returns a copy with all top-level object fields whose value is JNull removed.

Inherited from Json.

Implementation
dart
Json dropNullValues() => mapObject((a) => a.filter((keyValue) => !keyValue.$2.isNull));

fold() inherited

A fold<A>(
  A Function() jsonNull,
  A Function(bool) jsonBoolean,
  A Function(num) jsonNumber,
  A Function(String) jsonString,
  A Function(IList<Json>) jsonArray,
  A Function(JsonObject) jsonObject,
)

Pattern-matches this value, calling the corresponding function for each subtype.

Inherited from Json.

Implementation
dart
A fold<A>(
  Function0<A> jsonNull,
  Function1<bool, A> jsonBoolean,
  Function1<num, A> jsonNumber,
  Function1<String, A> jsonString,
  Function1<IList<Json>, A> jsonArray,
  Function1<JsonObject, A> jsonObject,
) => switch (this) {
  JNull _ => jsonNull(),
  final JBoolean b => jsonBoolean(b.value),
  final JNumber n => jsonNumber(n.value),
  final JString s => jsonString(s.value),
  final JArray a => jsonArray(a.value),
  final JObject o => jsonObject(o.value),
};

foldWith() override

A foldWith<A>(JsonFolder<A> folder)

Dispatches to folder based on the runtime subtype of this value.

Implementation
dart
@override
A foldWith<A>(JsonFolder<A> folder) => folder.onString(value);

mapArray() override

Json mapArray(IList<Json> Function(IList<Json>) f)

If this is JArray, returns a JArray with the elements mapped through f; otherwise returns this unchanged.

Implementation
dart
@override
Json mapArray(Function1<IList<Json>, IList<Json>> f) => this;

mapBoolean() override

Json mapBoolean(bool Function(bool) f)

If this is JBoolean, returns a JBoolean with the value mapped through f; otherwise returns this unchanged.

Implementation
dart
@override
Json mapBoolean(Function1<bool, bool> f) => this;

mapNumber() override

Json mapNumber(num Function(num) f)

If this is JNumber, returns a JNumber with the value mapped through f; otherwise returns this unchanged.

Implementation
dart
@override
Json mapNumber(Function1<num, num> f) => this;

mapObject() override

Json mapObject(JsonObject Function(JsonObject) f)

If this is JObject, returns a JObject with the JsonObject mapped through f; otherwise returns this unchanged.

Implementation
dart
@override
Json mapObject(Function1<JsonObject, JsonObject> f) => this;

mapString() override

Json mapString(String Function(String) f)

If this is JString, returns a JString with the value mapped through f; otherwise returns this unchanged.

Implementation
dart
@override
Json mapString(Function1<String, String> f) => JString(f(value));

noSuchMethod() inherited

dynamic noSuchMethod(Invocation invocation)

Invoked when a nonexistent method or property is accessed.

A dynamic member invocation can attempt to call a member which doesn't exist on the receiving object. Example:

dart
dynamic object = 1;
object.add(42); // Statically allowed, run-time error

This invalid code will invoke the noSuchMethod method of the integer 1 with an Invocation representing the .add(42) call and arguments (which then throws).

Classes can override noSuchMethod to provide custom behavior for such invalid dynamic invocations.

A class with a non-default noSuchMethod invocation can also omit implementations for members of its interface. Example:

dart
class MockList<T> implements List<T> {
  noSuchMethod(Invocation invocation) {
    log(invocation);
    super.noSuchMethod(invocation); // Will throw.
  }
}
void main() {
  MockList().add(42);
}

This code has no compile-time warnings or errors even though the MockList class has no concrete implementation of any of the List interface methods. Calls to List methods are forwarded to noSuchMethod, so this code will log an invocation similar to Invocation.method(#add, [42]) and then throw.

If a value is returned from noSuchMethod, it becomes the result of the original invocation. If the value is not of a type that can be returned by the original invocation, a type error occurs at the invocation.

The default behavior is to throw a NoSuchMethodError.

Inherited from Object.

Implementation
dart
@pragma("vm:entry-point")
@pragma("wasm:entry-point")
external dynamic noSuchMethod(Invocation invocation);

printWith() inherited

String printWith(Printer printer)

Renders this value using printer.

Inherited from Json.

Implementation
dart
String printWith(Printer printer) => printer.print(this);

toString() override

String toString()

A string representation of this object.

Some classes have a default textual representation, often paired with a static parse function (like int.parse). These classes will provide the textual representation as their string representation.

Other classes have no meaningful textual representation that a program will care about. Such classes will typically override toString to provide useful information when inspecting the object, mainly for debugging or logging.

Implementation
dart
@override
String toString() => 'JString("$value")';

withArray() override

Json withArray(Json Function(IList<Json>) f)

If this is JArray, returns f(elements); otherwise returns this unchanged.

Implementation
dart
@override
Json withArray(Function1<IList<Json>, Json> f) => this;

withBoolean() override

Json withBoolean(Json Function(bool) f)

If this is JBoolean, returns f(value); otherwise returns this unchanged.

Implementation
dart
@override
Json withBoolean(Function1<bool, Json> f) => this;

withNull() override

Json withNull(Json Function() f)

If this is JNull, returns f(); otherwise returns this unchanged.

Implementation
dart
@override
Json withNull(Function0<Json> f) => this;

withNumber() override

Json withNumber(Json Function(num) f)

If this is JNumber, returns f(value); otherwise returns this unchanged.

Implementation
dart
@override
Json withNumber(Function1<num, Json> f) => this;

withObject() override

Json withObject(Json Function(JsonObject) f)

If this is JObject, returns f(object); otherwise returns this unchanged.

Implementation
dart
@override
Json withObject(Function1<JsonObject, Json> f) => this;

withString() override

Json withString(Json Function(String) f)

If this is JString, returns f(value); otherwise returns this unchanged.

Implementation
dart
@override
Json withString(Function1<String, Json> f) => f(value);

Operators

operator ==() inherited

bool operator ==(Object other)

The equality operator.

The default behavior for all Objects is to return true if and only if this object and other are the same object.

Override this method to specify a different equality relation on a class. The overriding method must still be an equivalence relation. That is, it must be:

  • Total: It must return a boolean for all arguments. It should never throw.

  • Reflexive: For all objects o, o == o must be true.

  • Symmetric: For all objects o1 and o2, o1 == o2 and o2 == o1 must either both be true, or both be false.

  • Transitive: For all objects o1, o2, and o3, if o1 == o2 and o2 == o3 are true, then o1 == o3 must be true.

The method should also be consistent over time, so whether two objects are equal should only change if at least one of the objects was modified.

If a subclass overrides the equality operator, it should override the hashCode method as well to maintain consistency.

Inherited from Json.

Implementation
dart
@override
bool operator ==(Object other) {
  return fold(
    () => other is JNull,
    (boolean) => other is JBoolean && other.value == boolean,
    (number) => other is JNumber && other.value == number,
    (string) => other is JString && other.value == string,
    (ilist) => other is JArray && other.value == ilist,
    (object) => other is JObject && other.value == object,
  );
}