Json Data types and Properties Overview
Note
This feature is available in NCache Enterprise and Professional editions.
JSON is human readable lightweight syntax for the sharing of data. It is simply structured and formatted for machines to parse easily, and being a really helpful language for data interchange, it is used by various users.
JSON has the basic structures as:
- JSON Object: Unordered Name/Value pairs as attributes termed as JObject.
- JSON Array: Ordered list of Values termed JArray.
Where a Value could be a string, number, Boolean flag, JSON Object or even another JSON Array.
NCache lets you add data that represents JSON in NCache's domain, into your cache. The only requirement for adding data as JSON is for the cache to be JSON serialized.
The major ease provided by NCache to the user through Cache Data as JSON is the flexibility of retrieving data of any custom class in your cache as JSON. Moreover, data can be added as JSON and retrieved as a custom class, provided that the attributes represent the properties of the custom class. Data being JSON serialized, when retrieved by user as JSON is parsed by NCache and provided to you as either of the following according to your own requirement:
JsonValue
JsonObject
JsonArray
JsonNull
All these classes are derived from an abstract class by the name JsonValueBase
.
Consider a .NET class Product containing the data of products. When added with JsonObject the attributes of the class e.g. ProductName and ProductID etc, will be the attributes of the JsonObject. Given below, is the data of a .NET class containing the following properties:
Product product = new Product();
product.ProductID = 1001;
product.ProductName = "Chai";
product.Category = "Beverages"
The JSON equivalent of the given data when added as JsonObject will be a string containing all the attributes and is displayed as follows:
{
"ProductID" : 1001,
"ProductName" : "Chai",
"Category" : "Beverages"
}
Moreover, you can provide a string containing any Json data and parse it to get JsonValueBase in return. It will parse the string provided and show the datatype of the json data. Let us get a closer look at all these three classes provided by NCache.
JsonValue
This class maps values other than JObject
and JArray
in JSON standards to primitive values in NCache’s domain including additional types such as string
, DateTime
and decimal
. It basically represents primitive data in JSON conventions.
JsonObject
This class represents JObject
in JSON standards in NCache’s domain. Just as a JObject
contains name/value pairs where name is the name of the attribute and the value is the value of the particular attribute, this class is also composed of attributes in the form of string
and JsonValueBase
key-value pairs.
JsonArray
This class represents JArray
in JSON standards in NCache’s domain. It also maps arrays or list like collections in NCache’s domain to JArray according to JSON conventions. Moreover, just like a JArray
in JSON standards contains JObjects
or Values or even JArray, this class can be constructed to hold JsonValueBase
instances that takes into account JsonValue
, JsonObject
or even other JsonArray
instances.
JsonNull
This class represents null value in JSON standards. It also maps null values in NCache’s domain to null values in JSON standards.
JsonObject
and JsonArray
can be added with CacheItem
in the cache. CacheItem
is a class that represents a cached item including its various properties. Using CacheItem
, you can provide many functionalities to JsonValue
, JsonObject
or JsonArray
such as:
- Cache Dependencies (Enterprise only)
- Tags (Enterprise only)
- NamedTags (Enterprise only)
- Groups (Enterprise only)
- Expiration
Various operations performed using the above classes on the cache are explained in the successive chapters.
See Also
Using JsonValue in Cache
Using JsonObject in Cache
Using JsonArray in Cache
Cache Serialization Format