Skip to content

Commit

Permalink
Trim json before reading it
Browse files Browse the repository at this point in the history
  • Loading branch information
alelom authored and Fraser Greenroyd committed Jul 19, 2023
1 parent 132990d commit c0ae917
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions Serialiser_Engine/Convert/Json.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
using System;
using System.Collections;
using BH.oM.Base;
using BH.Engine.Base;

namespace BH.Engine.Serialiser
{
Expand Down Expand Up @@ -63,11 +64,13 @@ public static string ToJson(this object obj)
[Output("obj", "Object recovered from the Json string")]
public static object FromJson(string json)
{
if (json == "")
{
if (string.IsNullOrWhiteSpace(json))
return null;
}
else if (json.StartsWith("{"))

// Trim the json to remove any trailing or initial whitespace/newline (#3109).
json = json.Trim();

if (json.StartsWith("{"))
{
BsonDocument document;
if (BsonDocument.TryParse(json, out document))
Expand All @@ -87,7 +90,6 @@ public static object FromJson(string json)

return FromJsonArray(json);
}

else
{
// Could we do something when a string is not a valid json?
Expand Down

0 comments on commit c0ae917

Please sign in to comment.