Bunny83 / SimpleJSON

A simple JSON parser in C#
MIT License
735 stars 294 forks source link

Decimal and Datetime types #20

Closed BSarmady closed 3 years ago

BSarmady commented 6 years ago

Converting to DateTime and Decimal types are missing, I suggest following code but I'm not sure if it will work correctly.

    public virtual decimal AsDecimal {
        get {
            decimal v = 0.0M;
            if (decimal.TryParse(Value, NumberStyles.Float, CultureInfo.InvariantCulture, out v))
                return v;
            return 0.0M;
        }
        set {
            Value = value.ToString(CultureInfo.InvariantCulture);
        }
    }

    public virtual DateTime AsDateTime {
        get {
            DateTime d = DateTime.MinValue;
            if (DateTime.TryParse(Value, CultureInfo.InvariantCulture, DateTimeStyles.None, out d))
                return d;
            return DateTime.MinValue;
        }
        set {
            Value = value.ToString(CultureInfo.InvariantCulture);
        }
    }
devingDev commented 6 years ago

Need decimal as well !

devingDev commented 6 years ago

@bobsort your methods fail. I have this value in my JSON / MySQL : 42.1234567890123456 using your method I get : 42.123456789012

So it is missing the whole value. Seems like its a float which is not what I wanted. I mean I would then just use a float right? But I need precision of the decimal data type.

devingDev commented 6 years ago

Nevermind. My fault. Didn't check the input.

I was doing this : echo json_encode($mydata, JSON_NUMERIC_CHECK ); and this cut off decimal places.

echo json_encode($mydata); works as expected (side effect is every number is encapsulated with " )

Bunny83 commented 6 years ago

works as expected (side effect is every number is encapsulated with " )

This is exactly what the JSON standard is suggesting. The JSON standard does not have any requirement on the precision of numbers. Double is the most widely used implementation. If you must ensure to not loose any precision you should store your value as string and parse it yourself the way you need it.

SimpleJSON was mainly written for the use in the Unity 3d engine. You barely need more precision than a single precision float. It wouldn't make much sense to implement conversion operators / properties for every possible type out there. If you need a certain conversion you can write an extension for your needs. All classes are declared as partial so you can simply add new methods, operators or properties with an extending class.

devingDev commented 6 years ago

Anyway his decimal function works

Bunny83 commented 3 years ago

I've added a seperate extension file for .NET types including decimal, DateTime, TimeSpan, Guid and some others in the latest update. 029f124f8fc7dfc2db50e605a0f0bebfcccb58bd

This should close the gap. I don't want to add too many automatic conversions to the base framework as it will just blow up the size of the code. The extension files just need to be placed alongside the main file. (partial class abuse :) ).