Proposed API changes #53
Replies: 2 comments
-
Hi, thank you for your suggestions. In an early prototype of LightJson I had actually used the approach you described for converting values. However, after some internal testing I was seeing that it let to bugs where a I found the a more robust approach was to have a way to do implicit conversion and a way to do explicit conversions at any time regardless of the underlying The way I came up with is that if the client code wants to do implicit conversion, then it would use a property such as
These properties perform implicit conversion using the same rules as JavaScript when the types don't exactly match. For example: JsonValue.Parse("\"A non-empty string\"").AsBoolean // True
JsonValue.Parse("42").AsBoolean // True
JsonValue.Parse("\"not a number\"").AsNumber // 0 If you need an exception whenever the types don't match what you expect, then you should use the explicit conversion operator: (bool)(JsonValue.Parse("42")) // Throws Actually, I was just looking at the code right now and it seems that this behavior was changed, too. Ooops... So, exceptions don't get thrown anymore. The only thing that changed is that if the values can't be converted, you just get the default value for the type. I don't remember when or why that was changed, but I think changing that behavior at this time would break existing code using this library. |
Beta Was this translation helpful? Give feedback.
-
You may notice that I even created a separate repository, a fork of your project, with some adjustments that I found interesting to make, in my opinion. For example, one of the implementations was an explicit syntax to get objects in their given types and a check for when they are null. string jsonObj = """
{
"foo": 123
}
""";
var obj = JsonValue.Parse(jsonObj).GetJsonObject();
obj["foo"].GetString(); // Error: Expected to read the JSON value at $.foo as String, but got Number instead
obj["foo"].GetInteger(); // 123
obj["bar"].GetString(); // Error: At value $.bar it is expected to have a String.
obj["bar"].MaybeNull()?.GetString(); // null In addition to the implementation of JsonValueType.Undefined, which is returned when accessing a key in an array/object that does not exist. |
Beta Was this translation helpful? Give feedback.
-
Hello!
I really liked the project. It is an JSON interpreter that does not need to be linked to types or schemas to work. I see it as a perfect alternative to other JSON libraries, especially for compiling with bflat, as it doesn't supports SourceGeneration.
With the little use I had, I felt some holes and I'm here to show the changes I made, and if it makes sense for the project, I can merge them.
There are the tweaks i made.
I've...
JsonOptions.PropertyNameCaseInsensitive
property, which it's name say its all.JsonOptions.ThrowOnInvalidCast
, which when trying to cast an value into another invalid type, it would throw an exception.Example:
To:
Additionally, I expanded the JsonValue(object) constructor to support constructing dynamic types, so you can:
What do you think? Does it makes sense?
Beta Was this translation helpful? Give feedback.
All reactions