forked from neo-project/neo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJBoolean.cs
More file actions
104 lines (89 loc) · 2.67 KB
/
Copy pathJBoolean.cs
File metadata and controls
104 lines (89 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright (C) 2015-2024 The Neo Project.
//
// JBoolean.cs file belongs to the neo project and is free
// software distributed under the MIT software license, see the
// accompanying file LICENSE in the main directory of the
// repository or http://www.opensource.org/licenses/mit-license.php
// for more details.
//
// Redistribution and use in source and binary forms with or without
// modifications are permitted.
using System.Text.Json;
namespace Neo.Json
{
/// <summary>
/// Represents a JSON boolean value.
/// </summary>
public class JBoolean : JToken
{
/// <summary>
/// Gets the value of the JSON token.
/// </summary>
public bool Value { get; }
/// <summary>
/// Initializes a new instance of the <see cref="JBoolean"/> class with the specified value.
/// </summary>
/// <param name="value">The value of the JSON token.</param>
public JBoolean(bool value = false)
{
this.Value = value;
}
public override bool AsBoolean()
{
return Value;
}
/// <summary>
/// Converts the current JSON token to a floating point number.
/// </summary>
/// <returns>The number 1 if value is <see langword="true"/>; otherwise, 0.</returns>
public override double AsNumber()
{
return Value ? 1 : 0;
}
public override string AsString()
{
return Value.ToString().ToLowerInvariant();
}
public override bool GetBoolean() => Value;
public override string ToString()
{
return AsString();
}
internal override void Write(Utf8JsonWriter writer)
{
writer.WriteBooleanValue(Value);
}
public override JToken Clone()
{
return this;
}
public static implicit operator JBoolean(bool value)
{
return new JBoolean(value);
}
public static bool operator ==(JBoolean left, JBoolean right)
{
return left.Value.Equals(right.Value);
}
public static bool operator !=(JBoolean left, JBoolean right)
{
return !left.Value.Equals(right.Value);
}
public override bool Equals(object? obj)
{
if (ReferenceEquals(this, obj))
{
return true;
}
if (obj is JBoolean other)
{
return this.Value.Equals(other.Value);
}
return false;
}
public override int GetHashCode()
{
return Value.GetHashCode();
}
}
}