Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Copilot No. Series] Add Json functionality for working with JSON data #716

Merged
merged 13 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/System Application/App/Json/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Provides tools for working with JSON data such as reading, writing and parsing JSON.
39 changes: 39 additions & 0 deletions src/System Application/App/Json/app.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"id": "645965f7-95bf-4ee9-bf97-84e45dc6c6d1",
"name": "Json",
"publisher": "Microsoft",
"brief": "Provides tools for working with JSON data.",
"description": "Provides tools for working with JSON data such as reading, writing and parsing JSON.",
"version": "25.0.0.0",
"privacyStatement": "https://go.microsoft.com/fwlink/?linkid=724009",
"EULA": "https://go.microsoft.com/fwlink/?linkid=2009120",
"help": "https://go.microsoft.com/fwlink/?linkid=2103698",
"url": "https://go.microsoft.com/fwlink/?linkid=724011",
"logo": "",
"dependencies": [
{
"id": "e31ad830-3d46-472e-afeb-1d3d35247943",
"name": "BLOB Storage",
"publisher": "Microsoft",
"version": "25.0.0.0"
},
{
"id": "0846d207-5dec-4c1b-afd8-6a25e1e14b9d",
"name": "Base64 Convert",
"publisher": "Microsoft",
"version": "25.0.0.0"
}
JesperSchulz marked this conversation as resolved.
Show resolved Hide resolved
],
"screenshots": [

],
"platform": "24.0.0.0",
"idRanges": [
{
"from": 5460,
"to": 5461
}
],
"target": "OnPrem",
"contextSensitiveHelpUrl": "https://learn.microsoft.com/dynamics365/business-central/"
}
64 changes: 64 additions & 0 deletions src/System Application/App/Json/src/Json.Codeunit.al
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// ------------------------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
// ------------------------------------------------------------------------------------------------

namespace System.Json;
JesperSchulz marked this conversation as resolved.
Show resolved Hide resolved
DmitryKatson marked this conversation as resolved.
Show resolved Hide resolved

codeunit 5460 Json
JesperSchulz marked this conversation as resolved.
Show resolved Hide resolved
{
Access = Public;
JesperSchulz marked this conversation as resolved.
Show resolved Hide resolved
InherentEntitlements = X;
InherentPermissions = X;

var
JsonImpl: Codeunit "Json Impl.";

/// <summary>
/// Initializes the JSON array with the specified JSON string.
/// </summary>
/// <param name="JSONString">The Json string</param>
procedure InitializeCollection(JSONString: Text)
begin
JsonImpl.InitializeCollection(JSONString);
end;

/// <summary>
/// Initializes the JSON object with the specified JSON string.
/// </summary>
/// <param name="JSONString">The Json string</param>
procedure InitializeObject(JSONString: Text)
begin
JsonImpl.InitializeObject(JSONString);
end;

/// <summary>
/// Returns the number of elements in the JSON array.
/// </summary>
procedure GetCollectionCount(): Integer
begin
exit(JsonImpl.GetCollectionCount());
end;

/// <summary>
/// Returns the JSON object at the specified index in the JSON array.
/// </summary>
/// <param name="Object">The JSON object</param>
/// <param name="Index">The index of the JSON object</param>
procedure GetObjectFromCollectionByIndex(var "Object": Text; Index: Integer): Boolean
begin
exit(JsonImpl.GetObjectFromCollectionByIndex("Object", Index));
end;

/// <summary>
/// Gets the value at the specified property path in the JSON object and sets it to the specified record field.
/// </summary>
/// <param name="RecordRef">The record reference</param>
/// <param name="PropertyPath">The property path</param>
/// <param name="FieldNo">The field number</param>

procedure GetValueAndSetToRecFieldNo(RecordRef: RecordRef; PropertyPath: Text; FieldNo: Integer): Boolean
DmitryKatson marked this conversation as resolved.
Show resolved Hide resolved
begin
exit(JsonImpl.GetValueAndSetToRecFieldNo(RecordRef, PropertyPath, FieldNo));
end;
}
246 changes: 246 additions & 0 deletions src/System Application/App/Json/src/JsonImpl.Codeunit.al
Original file line number Diff line number Diff line change
@@ -0,0 +1,246 @@
// ------------------------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
// ------------------------------------------------------------------------------------------------

namespace System.Json;
DmitryKatson marked this conversation as resolved.
Show resolved Hide resolved

using System;
using System.Text;
using System.Utilities;

codeunit 5461 "Json Impl."
{
Access = Internal;
InherentEntitlements = X;
InherentPermissions = X;

var
JsonArray: DotNet JArray;
JsonObject: DotNet JObject;


procedure InitializeCollection(JSONString: Text)
begin
InitializeCollectionFromString(JSONString);
end;

procedure InitializeObject(JSONString: Text)
begin
InitializeObjectFromString(JSONString);
end;

procedure GetCollectionCount(): Integer
begin
exit(JsonArray.Count);
end;

procedure GetObjectFromCollectionByIndex(var "Object": Text; Index: Integer): Boolean
var
JObject: DotNet JObject;
begin
if not GetJObjectFromCollectionByIndex(JObject, Index) then
exit(false);

Object := JObject.ToString();
exit(true);
end;

procedure GetValueAndSetToRecFieldNo(RecordRef: RecordRef; PropertyPath: Text; FieldNo: Integer): Boolean
var
FieldRef: FieldRef;
begin
if IsNull(JsonObject) then
exit(false);

FieldRef := RecordRef.Field(FieldNo);
exit(GetPropertyValueFromJObjectByPathSetToFieldRef(JsonObject, PropertyPath, FieldRef));
end;

local procedure InitializeCollectionFromString(JSONString: Text)
begin
Clear(JsonArray);
if JSONString <> '' then
JsonArray := JsonArray.Parse(JSONString)
else
InitializeEmptyCollection();
end;

local procedure InitializeObjectFromString(JSONString: Text)
begin
Clear(JsonObject);
if JSONString <> '' then
JsonObject := JsonObject.Parse(JSONString)
else
InitializeEmptyObject();
end;

local procedure GetJObjectFromCollectionByIndex(var JObject: DotNet JObject; Index: Integer): Boolean
begin
if (GetCollectionCount() = 0) or (GetCollectionCount() <= Index) then
exit(false);

JObject := JsonArray.Item(Index);
exit(not IsNull(JObject))
end;

local procedure GetPropertyValueFromJObjectByPathSetToFieldRef(JObject: DotNet JObject; propertyPath: Text; var FieldRef: FieldRef): Boolean
var
JProperty: DotNet JProperty;
RecID: RecordID;
Value: Variant;
DecimalVal: Decimal;
BoolVal: Boolean;
GuidVal: Guid;
DateVal: Date;
Success: Boolean;
IntVar: Integer;
begin
Success := false;
JProperty := JObject.SelectToken(propertyPath);

if IsNull(JProperty) then
exit(false);

Value := Format(JProperty.Value, 0, 9);

case FieldRef.Type of
FieldType::Integer,
FieldType::Decimal:
begin
Success := Evaluate(DecimalVal, Value, 9);
FieldRef.Value(DecimalVal);
end;
FieldType::Date:
begin
Success := Evaluate(DateVal, Value, 9);
FieldRef.Value(DateVal);
end;
FieldType::Boolean:
begin
Success := Evaluate(BoolVal, Value, 9);
FieldRef.Value(BoolVal);
end;
FieldType::GUID:
begin
Success := Evaluate(GuidVal, Value);
FieldRef.Value(GuidVal);
end;
FieldType::Text,
FieldType::Code:
begin
FieldRef.Value(CopyStr(Value, 1, FieldRef.Length));
Success := true;
end;
FieldType::Option:
begin
if not Evaluate(IntVar, Value) then
IntVar := TextToOptionValue(Value, FieldRef.OptionCaption);
if IntVar >= 0 then begin
FieldRef.Value := IntVar;
Success := true;
end;
end;
FieldType::BLOB:
if TryReadAsBase64(FieldRef, Value) then
Success := true;
FieldType::RecordID:
begin
Success := Evaluate(RecID, Value);
FieldRef.Value(RecID);
end;
end;

exit(Success);
end;

[TryFunction]
local procedure TryReadAsBase64(var BlobFieldRef: FieldRef; Value: Text)
var
Base64Convert: Codeunit "Base64 Convert";
TempBlob: Codeunit "Temp Blob";
RecordRef: RecordRef;
OutStream: OutStream;
begin
TempBlob.CreateOutStream(OutStream);
Base64Convert.FromBase64(Value, OutStream);
RecordRef := BlobFieldRef.Record();
TempBlob.ToRecordRef(RecordRef, BlobFieldRef.Number);
end;

local procedure TextToOptionValue(InputText: Text; OptionString: Text): Integer
var
IntVar: Integer;
Counter: Integer;
begin
if InputText = '' then
InputText := ' ';
darjoo marked this conversation as resolved.
Show resolved Hide resolved

if Evaluate(IntVar, InputText) then begin
if IntVar < 0 then
IntVar := -1;
if GetOptionsQuantity(OptionString) < IntVar then
IntVar := -1;
end else begin
IntVar := -1;
for Counter := 1 to GetOptionsQuantity(OptionString) + 1 do
if UpperCase(GetSubStrByNo(Counter, OptionString)) = UpperCase(InputText) then
IntVar := Counter - 1;
end;

exit(IntVar);
end;

local procedure GetOptionsQuantity(OptionString: Text): Integer
var
Counter: Integer;
CommaPosition: Integer;
begin
if StrPos(OptionString, ',') = 0 then
exit(0);

repeat
CommaPosition := StrPos(OptionString, ',');
OptionString := DelStr(OptionString, 1, CommaPosition);
Counter := Counter + 1;
until CommaPosition = 0;

exit(Counter - 1);
end;

local procedure GetSubStrByNo(Number: Integer; CommaString: Text) SelectedStr: Text
var
SubStrQuantity: Integer;
Counter: Integer;
CommaPosition: Integer;
begin
if Number <= 0 then
exit;

SubStrQuantity := GetOptionsQuantity(CommaString);
if SubStrQuantity + 1 < Number then
exit;

repeat
Counter := Counter + 1;
CommaPosition := StrPos(CommaString, ',');
if CommaPosition = 0 then
SelectedStr := CommaString
else begin
SelectedStr := CopyStr(CommaString, 1, CommaPosition - 1);
CommaString := DelStr(CommaString, 1, CommaPosition);
end;
until Counter = Number;
end;

local procedure InitializeEmptyCollection()
begin
JsonArray := JsonArray.JArray();
end;

local procedure InitializeEmptyObject()
begin
JsonObject := JsonObject.JObject();
end;
}

Loading