Skip to content

Instantly share code, notes, and snippets.

@thash
Created March 18, 2020 13:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thash/7ef8648673b438946fca3c82bf50d46d to your computer and use it in GitHub Desktop.
Save thash/7ef8648673b438946fca3c82bf50d46d to your computer and use it in GitHub Desktop.
function converter() {
this.isStandardJson = function(item) {
for (var attr in item) {
if (item.hasOwnProperty(attr)) {
var attrValue = item[attr];
if (isAttrStandardJson.call(this, attrValue) == false) {
return false;
}
}
}
return true;
}
function isAttrStandardJson(attr) {
if (attr === null) return false;
var keys = Object.keys(attr);
var type = keys[0];
if (isDynamoDBType(type)) {
return false;
} else if (type === "M") {
return this.isStandardJson(attr[type]);
} else if (type === "L") {
var list = attr[type];
var length = list.length;
for (var i = 0; i < length; i++) {
if (isAttrStandardJson.call(this,list[i]) == false) {
return false;
}
}
}
return true;
}
this.formatWireJson = function(item) {
var attrList = {};
for (var attribute in item) {
var keys = Object.keys(item[attribute]);
var type = keys[0];
var value = formatWireAttr(type, item[attribute]);
attrList[attribute] = value;
}
return attrList;
}
function formatWireRecursive(item, isArray) {
if (isArray) {
var attrList = [];
}
else {
var attrList = {};
}
for (var attribute in item) {
var keys = Object.keys(item[attribute]);
var type = keys[0];
var value = formatWireAttr(type, item[attribute]);
attrList[attribute] = value;
}
return attrList;
}
function trimLeadingZeros(str) {
if (str == null) {
return null;
}
if (str.length == 0) {
return 0;
}
var newStr = "";
var foundDigit = false;
var isNegative = false;
for (var i = 0; i < str.length; i++) {
var currChar = str.charAt(i);
if (currChar == '-') {
isNegative = true;
} else if (currChar == '+') {
continue;
} else if (currChar != '0') {
foundDigit = true;
}
if (foundDigit) {
if (newStr.length == 0 && currChar == '.') {
newStr += '0';
}
newStr += currChar;
}
}
// Handle the 0 case.
if (!foundDigit) {
return 0;
}
// Invalid JSON if it is just a trailing decimal.
if (str.charAt(str.length-1) == ".") {
newStr = newStr + "0";
}
if (isNegative) {
newStr = '-' + newStr;
}
return newStr;
}
function formatWireAttr(type, dict) {
switch (type) {
case "S":
case "BOOL":
return dict[type];
case "N":
// Lossless JSON parser does not support leading 0's.
try {
var trimmedNum = trimLeadingZeros(dict[type]);
return new LosslessJSON.LosslessNumber(trimmedNum);
}
catch(e)
{
return 0;
}
case "NULL":
return null;
case "L":
return formatWireRecursive(dict[type], true);
case "M":
return formatWireRecursive(dict[type], false);
default:
return dict;
}
}
this.formatConsoleJson = function(item) {
var attributeValueMap = {};
for (var attr in item) {
var value = item[attr];
attributeValueMap[attr] = formatConsoleAttr(value);
}
return attributeValueMap;
}
function formatConsoleAttr(item) {
if (isScalarType(item)) {
return formatScalarType(item);
} else if (isRecursiveType(item)) {
return formatRecursiveType(item);
} else {
return item;
}
}
function isScalarType(dataType) {
var type = typeof(dataType);
return type === "number"
|| type === "string"
|| type === "boolean"
|| dataType === null
|| dataType.isLosslessNumber;
}
function formatScalarType(dataType) {
if (dataType == null) {
return { "NULL" : true };
}
var type = typeof(dataType);
if (type === "string") {
return { "S" : dataType };
} else if (type === "number") {
return { "N" : String(dataType) };
} else if (dataType != null && dataType.isLosslessNumber) {
return { "N" : dataType.value }
} else if (type === "boolean") {
return { "BOOL" : dataType };
} else {
return { "S" : dataType };
}
}
function isDynamoDBType(type) {
return type === "B" ||
type === "SS" ||
type === "NS" ||
type === "BS";
}
function isRecursiveType(dataType) {
return Array.isArray(dataType) ||
typeof(dataType) === "object";
}
function formatRecursiveType(item) {
var obj = {};
var value = {};
var type = 'M';
if (Array.isArray(item)) {
value = [];
type = 'L';
}
for (var key in item) {
value[key] = formatConsoleAttr(item[key]);
}
obj[type] = value;
return obj;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment