This repository was archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Json string and boolean #40107
Merged
kasiabulat
merged 18 commits into
dotnet:master
from
kasiabulat:kasiabulat/json-string-and-boolean
Aug 10, 2019
Merged
Json string and boolean #40107
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
d61199d
API to implement added
kasiabulat 521f115
JsonBoolean implementation added
kasiabulat 2c63fe8
JsonString implementation added
kasiabulat f2c537d
JsonBoolean tests added
kasiabulat 681ce39
JsonString implementation started
kasiabulat c6f2788
more tests added, csproj files fixes
kasiabulat 628660f
minor changes
kasiabulat 4753074
work on review comments
kasiabulat 43108a0
Merge remote-tracking branch 'upstream/master' into kasiabulat/json-s…
kasiabulat af79bec
trailing whitespaces fixes
kasiabulat df377a1
review comments included
kasiabulat 577fc6f
minor fix
kasiabulat 6187df1
Merged with JsonNumber
kasiabulat d05d733
review comments included
kasiabulat 40c3b1e
100% lines and branches covered
kasiabulat d9a76c6
framework fixes
kasiabulat d14e1db
span fixes
kasiabulat 0157a2c
minor changes
kasiabulat File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
src/System.Text.Json/src/System/Text/Json/Node/JsonBoolean.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| namespace System.Text.Json | ||
| { | ||
| /// <summary> | ||
| /// Represents a boolean JSON value. | ||
| /// </summary> | ||
| public sealed class JsonBoolean : JsonNode, IEquatable<JsonBoolean> | ||
| { | ||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="JsonBoolean"/> class representing the value <see langword="false"/>. | ||
| /// </summary> | ||
| public JsonBoolean() => Value = false; | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="JsonBoolean"/> class representing a specified value. | ||
| /// </summary> | ||
| public JsonBoolean(bool value) => Value = value; | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the boolean value represented by the instance. | ||
| /// </summary> | ||
| public bool Value { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Converts the value represented by the instance to the string in JSON format. | ||
| /// </summary> | ||
| /// <returns>The string representation of the value of this instance.</returns> | ||
| public override string ToString() => Value ? "true" : "false"; | ||
|
|
||
| /// <summary> | ||
| /// Converts a <see cref="bool"/> to a <see cref="JsonBoolean"/>. | ||
| /// </summary> | ||
| /// <param name="value">The value to convert.</param> | ||
| public static implicit operator JsonBoolean(bool value) => new JsonBoolean(value); | ||
|
kasiabulat marked this conversation as resolved.
|
||
|
|
||
| /// <summary> | ||
| /// Compares <paramref name="obj"/> to the value of this instance. | ||
| /// </summary> | ||
| /// <param name="obj">The object to compare against.</param> | ||
| /// <returns> | ||
| /// <see langword="true"/> if the boolean value of this instance matches <paramref name="obj"/>, | ||
| /// <see langword="false"/> otherwise. | ||
| /// </returns> | ||
| public override bool Equals(object obj) => obj is JsonBoolean jsonBoolean && Equals(jsonBoolean); | ||
|
|
||
| /// <summary> | ||
| /// Calculates a hash code of this instance. | ||
| /// </summary> | ||
| /// <returns>A hash code for this instance.</returns> | ||
| public override int GetHashCode() => Value.GetHashCode(); | ||
|
|
||
| /// <summary> | ||
| /// Compares other JSON boolean to the value of this instance. | ||
| /// </summary> | ||
| /// <param name="other">The JSON boolean to compare against.</param> | ||
| /// <returns> | ||
| /// <see langword="true"/> if the boolean value of this instance matches <paramref name="other"/>, | ||
| /// <see langword="false"/> otherwise. | ||
| /// </returns> | ||
| public bool Equals(JsonBoolean other) => !(other is null) && Value == other.Value; | ||
|
|
||
| /// <summary> | ||
| /// Compares values of two JSON booleans. | ||
| /// </summary> | ||
| /// <param name="left">The JSON boolean to compare.</param> | ||
| /// <param name="right">The JSON boolean to compare.</param> | ||
| /// <returns> | ||
| /// <see langword="true"/> if values of instances match, | ||
| /// <see langword="false"/> otherwise. | ||
| /// </returns> | ||
| public static bool operator ==(JsonBoolean left, JsonBoolean right) | ||
| { | ||
| // Test "right" first to allow branch elimination when inlined for null checks (== null) | ||
| // so it can become a simple test | ||
| if (right is null) | ||
|
kasiabulat marked this conversation as resolved.
|
||
| { | ||
| // return true/false not the test result https://github.com/dotnet/coreclr/issues/914 | ||
| return (left is null) ? true : false; | ||
| } | ||
|
|
||
| return right.Equals(left); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Compares values of two JSON booleans. | ||
| /// </summary> | ||
| /// <param name="left">The JSON boolean to compare.</param> | ||
| /// <param name="right">The JSON boolean to compare.</param> | ||
| /// <returns> | ||
| /// <see langword="true"/> if values of instances do not match, | ||
| /// <see langword="false"/> otherwise. | ||
| /// </returns> | ||
| public static bool operator !=(JsonBoolean left, JsonBoolean right) => !(left == right); | ||
| } | ||
| } | ||
117 changes: 117 additions & 0 deletions
117
src/System.Text.Json/src/System/Text/Json/Node/JsonString.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| namespace System.Text.Json | ||
| { | ||
| /// <summary> | ||
| /// Represents a text JSON value. | ||
| /// </summary> | ||
| public sealed class JsonString : JsonNode, IEquatable<JsonString> | ||
| { | ||
| private string _value; | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="JsonString"/> class representing the empty value. | ||
| /// </summary> | ||
| public JsonString() => Value = string.Empty; | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="JsonString"/> class representing a specified value. | ||
| /// </summary> | ||
| /// <param name="value">The value to represent as a JSON string.</param> | ||
| /// <exception cref="ArgumentNullException"> | ||
| /// Provided value is null. | ||
| /// </exception> | ||
| public JsonString(string value) => Value = value; | ||
|
kasiabulat marked this conversation as resolved.
kasiabulat marked this conversation as resolved.
|
||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="JsonString"/> class representing a specified value. | ||
| /// </summary> | ||
| /// <param name="value">The value to represent as a JSON string.</param> | ||
| public JsonString(ReadOnlySpan<char> value) => Value = value.ToString(); | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the text value represented by the instance. | ||
| /// </summary> | ||
| /// <exception cref="ArgumentNullException"> | ||
| /// Provided value is null. | ||
| /// </exception> | ||
| public string Value | ||
| { | ||
| get => _value; | ||
| set => _value = value ?? throw new ArgumentNullException(); | ||
|
kasiabulat marked this conversation as resolved.
|
||
| } | ||
|
|
||
| /// <summary> | ||
| /// Returns the text value represented by the instance. | ||
| /// </summary> | ||
| /// <returns>The value represented by this instance.</returns> | ||
| public override string ToString() => _value; | ||
|
|
||
| /// <summary> | ||
| /// Converts a <see cref="string"/> to a <see cref="JsonString"/>. | ||
| /// </summary> | ||
| /// <param name="value">The value to convert.</param> | ||
| public static implicit operator JsonString(string value) => new JsonString(value); | ||
|
|
||
| /// <summary> | ||
| /// Compares <paramref name="obj"/> to the value of this instance. | ||
| /// </summary> | ||
| /// <param name="obj">The object to compare against.</param> | ||
| /// <returns> | ||
| /// <see langword="true"/> if the text value of this instance matches <paramref name="obj"/>, | ||
| /// <see langword="false"/> otherwise. | ||
| /// </returns> | ||
| public override bool Equals(object obj) => obj is JsonString jsonString && Equals(jsonString); | ||
|
|
||
| /// <summary> | ||
| /// Calculates a hash code of this instance. | ||
| /// </summary> | ||
| /// <returns>A hash code for this instance.</returns> | ||
| public override int GetHashCode() => Value.GetHashCode(); | ||
|
|
||
| /// <summary> | ||
| /// Compares other JSON string to the value of this instance. | ||
| /// </summary> | ||
| /// <param name="other">The JSON string to compare against.</param> | ||
| /// <returns> | ||
| /// <see langword="true"/> if the text value of this instance matches <paramref name="other"/>, | ||
| /// <see langword="false"/> otherwise. | ||
| /// </returns> | ||
| public bool Equals(JsonString other) => !(other is null) && Value == other.Value; | ||
|
kasiabulat marked this conversation as resolved.
|
||
|
|
||
| /// <summary> | ||
| /// Compares values of two JSON strings. | ||
| /// </summary> | ||
| /// <param name="left">The JSON string to compare.</param> | ||
| /// <param name="right">The JSON string to compare.</param> | ||
| /// <returns> | ||
| /// <see langword="true"/> if values of instances match, | ||
| /// <see langword="false"/> otherwise. | ||
| /// </returns> | ||
| public static bool operator ==(JsonString left, JsonString right) | ||
| { | ||
| // Test "right" first to allow branch elimination when inlined for null checks (== null) | ||
| // so it can become a simple test | ||
| if (right is null) | ||
| { | ||
| // return true/false not the test result https://github.com/dotnet/coreclr/issues/914 | ||
| return (left is null) ? true : false; | ||
| } | ||
|
|
||
| return right.Equals(left); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Compares values of two JSON strings. | ||
| /// </summary> | ||
| /// <param name="left">The JSON string to compare.</param> | ||
| /// <param name="right">The JSON string to compare.</param> | ||
| /// <returns> | ||
| /// <see langword="true"/> if values of instances do not match, | ||
| /// <see langword="false"/> otherwise. | ||
| /// </returns> | ||
| public static bool operator !=(JsonString left, JsonString right) => !(left == right); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.