Skip to content

Commit

Permalink
Clean up unused private/internal methods (#250)
Browse files Browse the repository at this point in the history
  • Loading branch information
torbacz authored Sep 28, 2022
1 parent 6bf7b9b commit 2192adf
Show file tree
Hide file tree
Showing 7 changed files with 0 additions and 495 deletions.
46 changes: 0 additions & 46 deletions nanoFramework.Json/JsonArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,51 +25,5 @@ public JsonArray(JsonToken[] values)
public int Length => Items.Length;

public JsonToken[] Items { get; }

public override string ToString()
{
// set up a SerializationContext object and Lock it (via Monitor)
EnterSerialization();

try
{
StringBuilder sb = new();

sb.Append('[');

int prefaceLength = 0;
bool first = true;

foreach (var item in Items)
{
if (!first)
{
if (sb.Length - prefaceLength > 72)
{
// Use minimalist JSON, pretty can be handled by the client!
sb.Append(",");

prefaceLength = sb.Length;
}
else
{
sb.Append(',');
}
}

first = false;

sb.Append(item);
}

sb.Append(']');

return sb.ToString();
}
finally
{
ExitSerialization(); // Unlocks the SerializationContext object
}
}
}
}
68 changes: 0 additions & 68 deletions nanoFramework.Json/JsonObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,79 +16,11 @@ internal class JsonObject : JsonToken
{
private readonly Hashtable _members = new();

public JsonProperty this[string name]
{
get { return (JsonProperty)_members[name.ToLower()]; }
set
{
if (name.ToLower() != value.Name.ToLower())
{
throw new ArgumentException("index value must match property name");
}

_members.Add(value.Name.ToLower(), value);
}
}

public bool Contains(string name) => this._members.Contains(name.ToLower());

public ICollection Members => _members.Values;

public static object JsonObjectAttribute { get; private set; }

public void Add(string name, JsonToken value)
{
_members.Add(name.ToLower(), new JsonProperty(name, value));
}

//Use minimalist JSON, pretty can be handled on the client!
public override string ToString()
{
// set up a SerializationContext object and Lock it (via Monitor)
EnterSerialization();

try
{
StringBuilder sb = new();

sb.Append("{");

bool first = true;
Type type;

foreach (var key in _members.Keys)
{
var member = _members[key];
if (!first)
{
sb.Append(",");
}

first = false;

type = member.GetType();
if (type == typeof(JsonProperty))
{
sb.Append(((JsonProperty)member).ToString());
}
else if (type == typeof(JsonObject))
{
sb.Append($"\"{key}\":{(JsonObject)member}");
}
else if (type == typeof(JsonArray))
{
sb.Append(((JsonArray)member).ToString());
}
}

sb.Append("}");

return sb.ToString();
}
finally
{
ExitSerialization(); // Unlocks the SerializationContext object
}
}
}
}
35 changes: 0 additions & 35 deletions nanoFramework.Json/JsonProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,40 +22,5 @@ public JsonProperty(string name, JsonToken value)

public string Name { get; set; }
public JsonToken Value { get; set; }

public override string ToString()
{
EnterSerialization();

StringBuilder sb = new(); //TODO: why move out of the try?

try
{
//Use minimalist JSON, pretty can be handled on the client!
sb.Append('"');
sb.Append(Name);
sb.Append("\":");

JsonToken token = Value;

if (token is JsonValue j
&& j.Value != null
&& j.Value.GetType().Name == "Boolean")
{
// need to convert Boolean values to lower case
sb.Append(Value.ToString().ToLower());

return sb.ToString();
}

sb.Append(Value.ToString());

return sb.ToString();
}
finally
{
ExitSerialization();
}
}
}
}
67 changes: 0 additions & 67 deletions nanoFramework.Json/JsonToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,73 +12,6 @@ namespace nanoFramework.Json
{
internal abstract class JsonToken
{
private static object SyncObj = new();

private bool _fOwnsContext;

internal class SerializationCtx
{
public int Indent;
}

internal static SerializationCtx SerializationContext = null;

protected void EnterSerialization()
{
lock (SyncObj)
{
if (SerializationContext != null)
{
return;
}

SerializationContext = new SerializationCtx
{
Indent = 0
};

Monitor.Enter(SerializationContext);

_fOwnsContext = true;
}
}

protected void ExitSerialization()
{
lock (SyncObj)
{
if (!_fOwnsContext)
{
return;
}

var monitorObj = SerializationContext;
SerializationContext = null;
_fOwnsContext = false;

Monitor.Exit(monitorObj);
}
}

protected void MarshallEName(string ename, byte[] buffer, ref int offset)
{
byte[] name = Encoding.UTF8.GetBytes(ename);

if (buffer != null && ename.Length > 0)
{
Array.Copy(name, 0, buffer, offset, name.Length);
}

offset += name.Length;

if (buffer != null)
{
buffer[offset] = 0;
}

++offset;
}

public static string ConvertToString(byte[] byteArray, int start, int count)
{
var _chars = new char[byteArray.Length];
Expand Down
55 changes: 0 additions & 55 deletions nanoFramework.Json/JsonValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,60 +30,5 @@ public JsonValue(object value, bool isDateTime = false)
}

public object Value { get; set; }

public static JsonValue Serialize(Type type, object oValue)
{
if (type.Name == "Single" && float.IsNaN((float)oValue))
{
//Unfortunately JSON does not understand "float.NaN". This is the next best option!
return new JsonValue() { Value = null };
}
else if (type.Name == "Double" && double.IsNaN((double)oValue))
{
//Unfortunately JSON does not understand "float.NaN". This is the next best option!
return new JsonValue() { Value = null };
}

return new JsonValue() { Value = oValue };
}

public override string ToString()
{
EnterSerialization();

try
{
if (Value == null)
{
return "null";
}

var type = Value.GetType();

if (type == typeof(string)
|| type == typeof(char)
|| type == typeof(TimeSpan))
{
return "\"" + Value.ToString() + "\"";
}
else if (type == typeof(DateTime))
{
return "\"" + DateTimeExtensions.ToIso8601(((DateTime)Value)) + "\"";
}
else if (type == typeof(bool))
{
// need to convert Boolean values to lower case
return Value.ToString().ToLower();
}
else
{
return Value.ToString();
}
}
finally
{
ExitSerialization();
}
}
}
}
Loading

0 comments on commit 2192adf

Please sign in to comment.