codeunit 50390 "JSon Mgt.1.0"
{
trigger OnRun()
begin
end;
procedure GetJsonValue(var json_Object: JsonObject; Property: Text; var json_Value: JsonValue): Boolean
var
json_Token: JsonToken;
begin
if not json_Object.Get(Property, json_Token) then
exit;
json_Value := json_Token.AsValue();
exit(true);
end;
procedure GetJsonValueAsText(var json_Object: JsonObject; Property: Text) Value: Text
var
json_Value: JsonValue;
begin
if not GetJsonValue(json_Object, Property, json_Value) then
exit;
Value := json_Value.AsText;
end;
procedure GetJsonValueAsBoolean(var json_Object: JsonObject; Property: Text) Value: Boolean
var
json_Value: JsonValue;
begin
if not GetJsonValue(json_Object, Property, json_Value) then
exit;
Value := json_Value.AsBoolean();
end;
procedure GetJsonValueAsInteger(var json_Object: JsonObject; Property: Text) Value: Integer
var
json_Value: JsonValue;
begin
if not GetJsonValue(json_Object, Property, json_Value) then
exit;
Value := json_Value.AsInteger();
end;
procedure GetJsonValueAsDecimal(var json_Object: JsonObject; Property: Text) Value: Decimal
var
json_Value: JsonValue;
begin
if not GetJsonValue(json_Object, Property, json_Value) then
exit;
Value := json_Value.AsDecimal();
end;
procedure GetJsonValueAsDateTime(var json_Object: JsonObject; Property: Text) Value: DateTime
var
json_Value: JsonValue;
begin
if not GetJsonValue(json_Object, Property, json_Value) then
exit;
Value := json_Value.AsDateTime();
end;
procedure GetJsonValueAsDate(var json_Object: JsonObject; Property: Text) Value: Date
var
json_Value: JsonValue;
begin
if not GetJsonValue(json_Object, Property, json_Value) then
exit;
Value := json_Value.AsDate();
end;
procedure GetJsonValueAsTime(var json_Object: JsonObject; Property: Text) Value: Time
var
json_Value: JsonValue;
begin
if not GetJsonValue(json_Object, Property, json_Value) then
exit;
Value := json_Value.AsTime();
end;
procedure GetJsonValueAsCode(var json_Object: JsonObject; Property: Text) Value: Code[2000]
var
json_Value: JsonValue;
begin
if not GetJsonValue(json_Object, Property, json_Value) then
exit;
Value := json_Value.AsCode();
end;
procedure GetJsonValueAsDuration(var json_Object: JsonObject; Property: Text) Value: Duration
var
json_Value: JsonValue;
begin
if not GetJsonValue(json_Object, Property, json_Value) then
exit;
Value := json_Value.AsDuration();
end;
procedure GetJsonValueAsChar(var json_Object: JsonObject; Property: Text) Value: Char
var
json_Value: JsonValue;
begin
if not GetJsonValue(json_Object, Property, json_Value) then
exit;
Value := json_Value.AsChar();
end;
procedure GetJsonValueAsByte(var json_Object: JsonObject; Property: Text) Value: Byte
var
json_Value: JsonValue;
begin
if not GetJsonValue(json_Object, Property, json_Value) then
exit;
Value := json_Value.AsByte();
end;
procedure GetJsonToken(json_Object: JsonObject; tokenKey: Text) json_Token: JsonToken;
begin
if not json_Object.Get(tokenKey, json_Token) then
Error('Token not found with key %1', tokenKey);
end;
procedure SelectJsonToken(json_object: JsonObject; path: Text) json_Token: JsonToken;
begin
if not json_object.SelectToken(path, json_Token) then
Error('Token not found with path %1', path);
end;
/***********************************************************xml************/
procedure CreateBaseXMLDoc(NamespacePrefix: Text; NamespaceUri: Text): XmlDocument
var
XMLDoc: XmlDocument;
XMLDec: XmlDeclaration;
XMLElem: XmlElement;
XMLElem2: XmlElement;
XMLAtr: XmlAttribute;
begin
XMLDoc := XmlDocument.Create();
XMLDec := XmlDeclaration.Create('1.0', 'UTF-8', 'yes');
XMLDoc.SetDeclaration(XMLDec);
xmlElem := xmlElement.Create('Envelope', NamespaceUri);
XMLAtr := XmlAttribute.CreateNamespaceDeclaration('xsi', 'http://www.w3.org/2001/XMLSchema-instance');
XMLElem.Add(XMLAtr);
XMLAtr := XmlAttribute.CreateNamespaceDeclaration('xsd', 'http://www.w3.org/2001/XMLSchema');
XMLElem.Add(XMLAtr);
XMLAtr := XmlAttribute.CreateNamespaceDeclaration(NamespacePrefix, NamespaceUri);
XMLElem.Add(XMLAtr);
xmlElem2 := XmlElement.Create('Header', NamespaceUri);
xmlElem.Add(xmlElem2);
Clear(xmlElem2);
xmlElem2 := XmlElement.Create('Body', NamespaceUri);
xmlElem2.Add(xmlText.Create(''));
xmlElem.Add(xmlElem2);
xmlDoc.Add(xmlElem);
exit(XMLDoc);
end;
local procedure AddChildElementWithTxtValue(var inXMLElem: XmlElement; LocalName: Text; NamespaceUri: Text; inValue: Text);
var
CData: XmlCData;
begin
CData := XmlCData.Create('');
AddChildElementWithCdataTxtValue(inXMLElem, LocalName, NamespaceUri, inValue, CData)
end;
local procedure AddChildElementWithCdataTxtValue(var inXMLElem: XmlElement; LocalName: Text; NamespaceUri: Text; inValue: Text; CData: XmlCData)
var
XMLEelem: XmlElement;
begin
if NamespaceUri <> '' then
XMLEelem := XmlElement.Create(LocalName, NamespaceUri)
else
XMLEelem := XmlElement.Create(LocalName);
XMLEelem.Add(xmlText.Create(inValue));
if CData.Value() <> '' then
XMLEelem.Add(CData);
inXMLElem.Add(XMLEelem);
end;
procedure GetValueFromXML(Content: Text; pNodePath: Text): Text
var
XMLRootNode: XmlNode;
XMLChildNode: XmlNode;
XMLElem: XmlElement;
begin
GetRootNode(ConvertTextToXmlDocument(Content), XMLRootNode);
XMLRootNode.SelectSingleNode(pNodePath, XMLChildNode);
XMLElem := XMLChildNode.AsXmlElement();
exit(XMLElem.InnerText());
end;
procedure ConvertTextToXmlDocument(Content: Text): XmlDocument
var
XmlDoc: XmlDocument;
begin
if XmlDocument.ReadFrom(Content, XmlDoc) then
exit(XmlDoc);
end;
procedure GetRootNode(pXMLDocument: XmlDocument; var pFoundXMLNode: XmlNode)
var
lXmlElement: XmlElement;
begin
pXMLDocument.GetRoot(lXmlElement);
pFoundXMLNode := lXmlElement.AsXmlNode();
end;
var
SOAP11NamespaceURILbl: Label 'http://schemas.xmlsoap.org/soap/envelope/', Locked = true;
SOAP12NamespaceUriLbl: Label 'http://www.w3.org/2003/05/soap-envelope', Locked = true;
W3BaseNamespaceUriLbl: Label 'https://www.w3schools.com/xml/', Locked = true;
SOAP11XMLContentTypeLbl: Label 'text/xml; charset=utf-8', Locked = true;
SOAP12XMLContentTypeLbl: Label 'application/soap+xml; charset=utf-8', Locked = true;
W3ActionUriLbl: Label 'tempconvert.asmx', Locked = true;
LocalXPathSignatureLbl: Label '[local-name()="%1"]', Locked = true;
LocalXPathSeparatorLbl: Label '/*', Locked = true;
BodyXPathLbl: Label '/*[local-name()="Envelope"]/*[local-name()="Body"]', Locked = true;
ErrorLbl: Label 'Error', Locked = true;
var
JSOn: Codeunit "JSON Management";
}
No comments:
Post a Comment