Detecting Type of JSON Object
In C# we can use typeof to determine the object type e.g.
if("my string".GetType() == typeof(string))
{
// This is a string block
}
To achieve the same on a complex json object, I wrote this generic utility class in typescript to determine the instance type of the object.
export class Mapping {
/**
* Checks if the given json object is type of a given instance (class/interface) type.
* @param jsonObject Object to check.
* @param instanceType The type to check for the object.
* @returns true if object is of the given instance type; false otherwise.
*/
public static isTypeOf<T>(jsonObject: Object, instanceType: { new(): T; }): boolean {
// Check that all the properties of the JSON Object are also available in the Class.
const instanceObject = new instanceType();
for (let propertyName in instanceObject) {
if (!jsonObject.hasOwnProperty(propertyName)) {
// If any property in instance object is missing then we have a mismatch.
return false;
}
}
// All the properties are matching between object and the instance type.
return true;
};
/**
* Checks if the given json object is type of a given instance (class/interface) type.
* @param jsonObject Object to check.
* @param instanceType The type to check for the object.
* @returns true if object is of the given instance type; false otherwise.
*/
public static isCollectionTypeOf<T>(jsonObjectCollection: any[], instanceType: { new(): T; }): boolean {
// Check that all the properties of the JSON Object are also available in the Class.
const instanceObject = new instanceType();
for (let jsonObject of jsonObjectCollection) {
for (let propertyName in instanceObject) {
if (!jsonObject.hasOwnProperty(propertyName)) {
// If any property in instance object is missing then we have a mismatch.
return false;
}
}
}
// All the properties are matching between object and the instance type.
return true;
};
}; // End of class: Mapping
Hope someone benefits from this solution 🙌
Happy Coding
Thanks so much
You are welcome, glad that helped