forked from gui-cs/Terminal.Gui
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'v2_develop' of github.com:gui-cs/Terminal.Gui into v2_d…
…evelop
- Loading branch information
Showing
17 changed files
with
3,090 additions
and
3,170 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,99 +1,97 @@ | ||
using System; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
using Terminal.Gui; | ||
|
||
namespace Terminal.Gui { | ||
/// <summary> | ||
/// Json converter fro the <see cref="Attribute"/> class. | ||
/// </summary> | ||
class AttributeJsonConverter : JsonConverter<Attribute> { | ||
private static AttributeJsonConverter instance; | ||
namespace Terminal.Gui; | ||
|
||
/// <summary> | ||
/// | ||
/// </summary> | ||
public static AttributeJsonConverter Instance { | ||
get { | ||
if (instance == null) { | ||
instance = new AttributeJsonConverter (); | ||
} | ||
/// <summary> | ||
/// Json converter fro the <see cref="Attribute"/> class. | ||
/// </summary> | ||
class AttributeJsonConverter : JsonConverter<Attribute> { | ||
static AttributeJsonConverter _instance; | ||
|
||
return instance; | ||
/// <summary> | ||
/// | ||
/// </summary> | ||
public static AttributeJsonConverter Instance { | ||
get { | ||
if (_instance == null) { | ||
_instance = new AttributeJsonConverter (); | ||
} | ||
|
||
return _instance; | ||
} | ||
} | ||
|
||
public override Attribute Read (ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
if (reader.TokenType != JsonTokenType.StartObject) { | ||
throw new JsonException ($"Unexpected StartObject token when parsing Attribute: {reader.TokenType}."); | ||
} | ||
public override Attribute Read (ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
if (reader.TokenType != JsonTokenType.StartObject) { | ||
throw new JsonException ($"Unexpected StartObject token when parsing Attribute: {reader.TokenType}."); | ||
} | ||
|
||
Attribute attribute = new Attribute (); | ||
Color foreground = null; | ||
Color background = null; | ||
while (reader.Read ()) { | ||
if (reader.TokenType == JsonTokenType.EndObject) { | ||
if (foreground == null || background == null) { | ||
throw new JsonException ($"Both Foreground and Background colors must be provided."); | ||
} | ||
return new Attribute (foreground, background); | ||
var attribute = new Attribute (); | ||
Color? foreground = null; | ||
Color? background = null; | ||
while (reader.Read ()) { | ||
if (reader.TokenType == JsonTokenType.EndObject) { | ||
if (foreground == null || background == null) { | ||
throw new JsonException ("Both Foreground and Background colors must be provided."); | ||
} | ||
return new Attribute (foreground.Value, background.Value); | ||
} | ||
|
||
if (reader.TokenType != JsonTokenType.PropertyName) { | ||
throw new JsonException ($"Unexpected token when parsing Attribute: {reader.TokenType}."); | ||
} | ||
if (reader.TokenType != JsonTokenType.PropertyName) { | ||
throw new JsonException ($"Unexpected token when parsing Attribute: {reader.TokenType}."); | ||
} | ||
|
||
string propertyName = reader.GetString (); | ||
reader.Read (); | ||
string color = $"\"{reader.GetString ()}\""; | ||
var propertyName = reader.GetString (); | ||
reader.Read (); | ||
var color = $"\"{reader.GetString ()}\""; | ||
|
||
switch (propertyName.ToLower ()) { | ||
case "foreground": | ||
foreground = JsonSerializer.Deserialize<Color> (color, options); | ||
break; | ||
case "background": | ||
background = JsonSerializer.Deserialize<Color> (color, options); | ||
break; | ||
//case "bright": | ||
//case "bold": | ||
// attribute.Bright = reader.GetBoolean (); | ||
// break; | ||
//case "dim": | ||
// attribute.Dim = reader.GetBoolean (); | ||
// break; | ||
//case "underline": | ||
// attribute.Underline = reader.GetBoolean (); | ||
// break; | ||
//case "blink": | ||
// attribute.Blink = reader.GetBoolean (); | ||
// break; | ||
//case "reverse": | ||
// attribute.Reverse = reader.GetBoolean (); | ||
// break; | ||
//case "hidden": | ||
// attribute.Hidden = reader.GetBoolean (); | ||
// break; | ||
//case "strike-through": | ||
// attribute.StrikeThrough = reader.GetBoolean (); | ||
// break; | ||
default: | ||
throw new JsonException ($"Unknown Attribute property {propertyName}."); | ||
} | ||
switch (propertyName?.ToLower ()) { | ||
case "foreground": | ||
foreground = JsonSerializer.Deserialize<Color> (color, options); | ||
break; | ||
case "background": | ||
background = JsonSerializer.Deserialize<Color> (color, options); | ||
break; | ||
//case "bright": | ||
//case "bold": | ||
// attribute.Bright = reader.GetBoolean (); | ||
// break; | ||
//case "dim": | ||
// attribute.Dim = reader.GetBoolean (); | ||
// break; | ||
//case "underline": | ||
// attribute.Underline = reader.GetBoolean (); | ||
// break; | ||
//case "blink": | ||
// attribute.Blink = reader.GetBoolean (); | ||
// break; | ||
//case "reverse": | ||
// attribute.Reverse = reader.GetBoolean (); | ||
// break; | ||
//case "hidden": | ||
// attribute.Hidden = reader.GetBoolean (); | ||
// break; | ||
//case "strike-through": | ||
// attribute.StrikeThrough = reader.GetBoolean (); | ||
// break; | ||
default: | ||
throw new JsonException ($"Unknown Attribute property {propertyName}."); | ||
} | ||
throw new JsonException (); | ||
} | ||
|
||
public override void Write (Utf8JsonWriter writer, Attribute value, JsonSerializerOptions options) | ||
{ | ||
writer.WriteStartObject (); | ||
writer.WritePropertyName (nameof(Attribute.Foreground)); | ||
ColorJsonConverter.Instance.Write (writer, value.Foreground, options); | ||
writer.WritePropertyName (nameof (Attribute.Background)); | ||
ColorJsonConverter.Instance.Write (writer, value.Background, options); | ||
|
||
writer.WriteEndObject (); | ||
} | ||
throw new JsonException (); | ||
} | ||
} | ||
|
||
public override void Write (Utf8JsonWriter writer, Attribute value, JsonSerializerOptions options) | ||
{ | ||
writer.WriteStartObject (); | ||
writer.WritePropertyName (nameof (Attribute.Foreground)); | ||
ColorJsonConverter.Instance.Write (writer, value.Foreground, options); | ||
writer.WritePropertyName (nameof (Attribute.Background)); | ||
ColorJsonConverter.Instance.Write (writer, value.Background, options); | ||
|
||
writer.WriteEndObject (); | ||
} | ||
} |
This file contains 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
Oops, something went wrong.