Interface IJsonService
Service to serialize/restore JSON. Get it using GetService < T >
It works for 2sxc/EAV data but can be used for any data which can be serialized/deserialized. Since it's a data-operation, we keep it in this namespace, even if most other things in this namespace are 2sxc-data objects.
Important This is simple object-string conversion. It doesn't change entity objects to be serializable. For that you should use the IConvertToEavLight which returns an object that can then be serialized.
[PublicApi]
public interface IJsonService
Remarks
Introduced in 2sxc 12.05. For previous versions of 2sxc, you can just write code to access Newtonsoft directly. For more control regarding serialization, also just work with Newtonsoft directly.
Internally it uses Newtonsoft and preserves the case of keys. In future the internal engine may change (like for .net core), but we'll ensure that the result remains consistent.
Methods
ToJson(object)
Convert an object to JSON.
If you need to add the JSON to HTML of a page, make sure you also use Html.Raw(...)
, otherwise it will be encoded and not usable in JavaScript.
string ToJson(object item)
Parameters
item
objectThe object to serialize
Returns
ToJson(object, int)
Convert an object to JSON - using nicer output / indentation.
If you need to add the JSON to HTML of a page, make sure you also use Html.Raw(...)
, otherwise it will be encoded and not usable in JavaScript.
string ToJson(object item, int indentation)
Parameters
item
objectThe object to serialize
indentation
intHow much to indent the json - we recommend 4. As of now, it will always use 4, no matter what you set (see remarks)
Returns
Remarks
Added in 2sxc 12.11
But as of 2sxc 12.11 we're still using an old Newtonsoft, so we cannot really control the indentation depth. If you call this, it will always indent using 4 spaces. In a future release we'll probably use a newer Newtonsoft with which we can then use the indentation as needed.
ToObject(string)
Convert a json to an anonymous object. This is a very technical thing to do, so only use it if you know why you're doing this.
object ToObject(string json)
Parameters
json
string
Returns
- object
ToTyped(string, NoParamOrder, string, bool?)
Creates a ITyped object from a json string.
Important
This only works on json strings which return an object.
If you pass in a simple json such as 27
or "hello"
or an array like [1, 2, 3]
it will throw an error.
For arrays, use ToTypedList(string, NoParamOrder, string, bool?).
[PublicApi]
ITyped ToTyped(string json, NoParamOrder noParamOrder = default, string fallback = null, bool? propsRequired = null)
Parameters
json
stringThe string containing json
noParamOrder
NoParamOrderfallback
stringAlternate string to use, if the original json can't parse. Can also be null or the word "error" if you would prefer an error to be thrown.
propsRequired
bool?make the resulting object strict, default
true
Returns
- ITyped
A dynamic object representing the original json. If it can't be parsed, it will parse the fallback, which by default is an empty empty dynamic object. If you provide null for the fallback, then you will get null back.
Remarks
New in 16.02
ToTypedList(string, NoParamOrder, string, bool?)
Creates a list of ITyped wrappers around an json string containing an array of objects.
Important
This only works on json strings which return an object.
If you pass in a simple json such as 27
or "hello"
or an array like [1, 2, 3]
it will throw an error.
For arrays, use ToTypedList(string, NoParamOrder, string, bool?).
IEnumerable<ITyped> ToTypedList(string json, NoParamOrder noParamOrder = default, string fallback = null, bool? propsRequired = null)
Parameters
json
stringThe string containing json
noParamOrder
NoParamOrderfallback
stringAlternate string to use, if the original json can't parse. Can also be null or the word "error" if you would prefer an error to be thrown.
propsRequired
bool?make the resulting object strict, default
true
Returns
- IEnumerable<ITyped>
A dynamic object representing the original json. If it can't be parsed, it will parse the fallback, which by default is an empty empty dynamic object. If you provide null for the fallback, then you will get null back.
Remarks
New in 16.04
To<T>(string)
Convert a JSON to a typed object.
T To<T>(string json)
Parameters
json
string
Returns
- T
Type Parameters
T