JSON and HTML Predefined Entities - Yash-777/SeleniumDriverAutomation GitHub Wiki
JavaScript Object Notation
used as a Hash map
JSON is built on two structures:
- A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array. An object is an UN-ordered set of name/value pairs. An object begins with { (left brace) and ends with } (right brace). Each name is followed by : (colon) and the name/value pairs are separated by , (comma).
- An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence. An array is an ordered collection of values. An array begins with [ (left bracket) and ends with ] (right bracket). Values are separated by , (comma).
Example
to create a string equivalent to an object by listing all attributes and values.
var arr = new Array();
arr[0] = 'Yash';
arr[1] = '777';
var obj = { 'app' : 'github' };
var json = new Object();
json['Key'] = 'key_val';
json.property = 'property_Val';
json['array'] = arr;
json['obj'] = obj;
console.log('JSON :', json);
O/P: JSON : Object {Key: "key_val", property: "property_Val", array: Array[2], obj: Object}
Converting Javascript Object to a simple JSON-string
The JSON.stringify() method converts a JavaScript value to a JSON string.
The jsonString is a serialized version of the input object, JSON is literally a string representation. It is just a string.
var jsonString = JSON.stringify( json );
console.log('JSON String: ', jsonString );
O/P: JSON String: {"Key":"key_val","property":"property_Val","array":["Yash","777"],"obj":{"app":"github"}}
The JSON.parse() method parses a JSON string, constructing the JavaScript value or object described by the string.
The jsonString is de-serialized to an identical looking object using JSON.parse()
var jsonObject = JSON.parse( jsonString );
console.log('JSON Object : ', jsonObject );
O/P: JSON Object : Object {Key: "key_val", property: "property_Val", array: Array[2], obj: Object}
Swap
keys and values of a JSON Obejct:
Example JOSN Format: {A : 1, B : 2, C : 3, D : 4}
.
To a swapJsonKeyValues() method We’ll pass an object as a parameter, We then create a new empty object with interchanging keys and values
and then return it.
var jsonObj = {A : 1, B : 2, C : 3, D : 4};
var jsonString = '{A : 1, B : 2, C : 3, D : 4}';
function swapJsonKeyValues( object ) {
var newobj = {};
for (var key in object ) {
if ( object.hasOwnProperty( key )) {
newobj[ object[key]] = key;
}
}
return newobj;
}
var jsonObject = swapJsonKeyValues( jsonObj );
console.log('JSON Object: ', stateAbbrs );
console.log('JSON String: ', JSON.stringify( jsonObject ) );
JSON keys count
function count( obj ) { return Object.keys( obj ).length; }
HTML Special Characters & its ESCAPE CODES
Reserved Characters must be escaped by HTML: We can use a character escape to represent any Unicode character [Ex: & - U+00026] in HTML, XHTML or XML using only ASCII characters. Numeric character references [Ex: ampersand(&) - &
] & Named character references [Ex: &
] are types of character escape used in markup
.
Original Character XML entity replacement XML numeric replacement
< < <
> > >
" " "
& & &
' ' '
To display HTML Tags as a normal form in web page we use <pre>
, <code>
tags or we can escape them. Escaping the string by replacing with any occurrence of the "&" character by the string "&" and any occurrences of the ">" character by the string ">". Ex: stackoverflow post
function escapeCharEntities() {
var map = {
"&": "&",
"<": "<",
">": ">",
"\"": """,
"'": "'"
};
return map;
}
var mapkeys = '', mapvalues = '';
var html = {
encodeRex : function () {
return new RegExp(mapkeys, 'gm');
},
decodeRex : function () {
return new RegExp(mapvalues, 'gm');
},
encodeMap : JSON.parse( JSON.stringify( escapeCharEntities () ) ),
decodeMap : JSON.parse( JSON.stringify( swapJsonKeyValues( escapeCharEntities () ) ) ),
encode : function ( str ) {
return str.replace(html.encodeRex(), function(m) { return html.encodeMap[m]; });
},
decode : function ( str ) {
return str.replace(html.decodeRex(), function(m) { return html.decodeMap[m]; });
}
};
function swapJsonKeyValues ( json ) {
var count = Object.keys( json ).length;
var obj = {};
var keys = '[', val = '(', keysCount = 1;
for(var key in json) {
if ( json.hasOwnProperty( key ) ) {
obj[ json[ key ] ] = key;
keys += key;
if( keysCount < count ) {
val += json[ key ]+'|';
} else {
val += json[ key ];
}
keysCount++;
}
}
keys += ']'; val += ')';
console.log( keys, ' == ', val);
mapkeys = keys;
mapvalues = val;
return obj;
}
console.log('Encode: ', html.encode('<input type="password" name="password" value=""/>') );
console.log('Decode: ', html.decode(html.encode('<input type="password" name="password" value=""/>')) );
O/P:
Encode: <input type="password" name="password" value=""/>
Decode: <input type="password" name="password" value=""/>