Skip to content

Commit

Permalink
Added the ability to parse JSON in the request body
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrienCastex committed Jun 15, 2017
1 parent c9812d6 commit cf0debb
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/helper/XML.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export interface XMLElement {
}
export declare abstract class XML {
static parse(xml: string | Int8Array): XMLElement;
static parseJSON(xml: string | Int8Array, compact?: boolean): XMLElement;
static parseXML(xml: string | Int8Array): XMLElement;
static toJSON(xml: string): string;
static toXML(xml: XMLElement | any, includeDeclaration?: boolean): string;
static createElement(name: string, attributes?: any, text?: string): {
Expand Down
17 changes: 17 additions & 0 deletions lib/helper/XML.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,23 @@ var XML = (function () {
function XML() {
}
XML.parse = function (xml) {
try {
return XML.parseXML(xml);
}
catch (_) {
try {
return XML.parseJSON(xml, true);
}
catch (_) {
return XML.parseJSON(xml, false);
}
}
};
XML.parseJSON = function (xml, compact) {
if (compact === void 0) { compact = true; }
return XML.parseXML(xmljs.json2xml(xml.toString(), { compact: compact }));
};
XML.parseXML = function (xml) {
var x = xmljs.xml2js(xml.constructor === String ? xml : new Buffer(xml).toString(), {
compact: false
});
Expand Down
24 changes: 24 additions & 0 deletions src/helper/XML.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,30 @@ export interface XMLElement
export abstract class XML
{
static parse(xml : string | Int8Array) : XMLElement
{
try
{
return XML.parseXML(xml);
}
catch(_)
{
try
{
return XML.parseJSON(xml, true);
}
catch(_)
{
return XML.parseJSON(xml, false);
}
}
}

static parseJSON(xml : string | Int8Array, compact : boolean = true) : XMLElement
{
return XML.parseXML(xmljs.json2xml(xml.toString(), { compact }));
}

static parseXML(xml : string | Int8Array) : XMLElement
{
const x = xmljs.xml2js(xml.constructor === String ? xml as string : new Buffer(xml as Int8Array).toString(), {
compact: false
Expand Down

0 comments on commit cf0debb

Please sign in to comment.