Skip to content

Commit

Permalink
[Copilot No. Series] Add Json functionality for working with JSON data (
Browse files Browse the repository at this point in the history
#716)

Add new Json module to provide tools for working with JSON data,
including reading, writing, and parsing JSON. Introduces codeunit for
initializing JSON array and object, retrieving element counts, fetching
objects by index, and getting values for specified record fields.
Implements methods to interact with JSON data using .NET types.

This is partial implementation of the`Base Application` `codeunit 5459
"Json management"`

<!-- Thank you for submitting a Pull Request. If you're new to
contributing to BCApps please read our pull request guideline below
* https://github.com/microsoft/BCApps/Contributing.md
-->
#### Summary 

This PR is required for the [Number Series
Copilot](#659) implementation
as we agreed
[here](#659 (comment))

#### Work Item(s) 
Fixes #715

Fixes
[AB#506714](https://dynamicssmb2.visualstudio.com/1fcb79e7-ab07-432a-a3c6-6cf5a88ba4a5/_workitems/edit/506714)

---------

Co-authored-by: Jesper Schulz-Wedde <[email protected]>
Co-authored-by: Darrick <[email protected]>
  • Loading branch information
3 people authored Mar 22, 2024
1 parent c638514 commit 858a482
Show file tree
Hide file tree
Showing 6 changed files with 1,100 additions and 0 deletions.
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.
45 changes: 45 additions & 0 deletions src/System Application/App/Json/app.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"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"
},
{
"id": "7e3b999e-1182-45d2-8b82-d5127ddba9b2",
"name": "DotNet Aliases",
"publisher": "Microsoft",
"version": "25.0.0.0"
}
],
"screenshots": [

],
"platform": "24.0.0.0",
"idRanges": [
{
"from": 5460,
"to": 5461
}
],
"target": "OnPrem",
"contextSensitiveHelpUrl": "https://learn.microsoft.com/dynamics365/business-central/"
}
226 changes: 226 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,226 @@
// ------------------------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
// ------------------------------------------------------------------------------------------------

namespace System.Text.Json;

codeunit 5460 Json
{
Access = Public;
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.InitializeCollectionFromString(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.InitializeObjectFromString(JSONString);
end;

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

/// <summary>
/// Returns the JSON array in text format.
/// </summary>
/// <returns>The JSON array in text format</returns>
procedure GetCollectionAsText(): Text
begin
exit(JsonImpl.GetCollectionAsText());
end;

/// <summary>
/// Returns the JSON array.
/// </summary>
/// <returns>The JSON array</returns>
procedure GetCollection(): JsonArray
begin
exit(JsonImpl.GetCollection());
end;

/// <summary>
/// Returns the JSON object in text format.
/// </summary>
/// <returns>The JSON object in text format</returns>
procedure GetObjectAsText(): Text
begin
exit(JsonImpl.GetObjectAsText());
end;

/// <summary>
/// Returns the JSON object.
/// </summary>
/// <returns>The JSON object</returns>
procedure GetObject(): JsonObject
begin
exit(JsonImpl.GetObject());
end;

/// <summary>
/// Returns the JSON object at the specified index in the JSON array.
/// </summary>
/// <param name="Index">The index of the JSON object</param>
/// <param name="JsonObjectTxt">The JSON object in text format</param>
/// <returns>True if the JSON object is returned; otherwise, false</returns>
procedure GetObjectFromCollectionByIndex(Index: Integer; var JsonObjectTxt: Text): Boolean
begin
exit(JsonImpl.GetObjectFromCollectionByIndex(Index, JsonObjectTxt));
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>
/// <returns>True if the value is set to the record field; otherwise, false</returns>
/// <remarks>
/// Next type of fields are supported: Integer, Decimal, Date, Boolean, GUID, Text, Code, Option, BLOB, RecordID
/// Text values are trimmed to the Max Length of the field.
/// </remarks>
procedure GetValueAndSetToRecFieldNo(RecordRef: RecordRef; PropertyPath: Text; FieldNo: Integer): Boolean
begin
exit(JsonImpl.GetValueAndSetToRecFieldNo(RecordRef, PropertyPath, FieldNo));
end;

/// <summary>
/// Gets the value at the specified property name in the JSON object.
/// </summary>
/// <param name="PropertyName">The property name</param>
/// <param name="Value">The value</param>
/// <returns>True if the value is returned; otherwise, false</returns>
procedure GetPropertyValueByName(PropertyName: Text; var Value: Variant): Boolean
begin
exit(JsonImpl.GetPropertyValueFromJObjectByName(PropertyName, Value));
end;

/// <summary>
/// Gets the text value at the specified property name in the JSON object.
/// </summary>
/// <param name="PropertyName">The property name</param>
/// <param name="Value">The value</param>
/// <returns>True if the value is returned; otherwise, false</returns>
procedure GetStringPropertyValueByName(PropertyName: Text; var Value: Text): Boolean
begin
exit(JsonImpl.GetStringPropertyValueFromJObjectByName(PropertyName, Value));
end;

/// <summary>
/// Gets the option value at the specified property name in the JSON object.
/// </summary>
/// <param name="PropertyName">The property name</param>
/// <param name="Value">The value</param>
/// <returns>True if the value is returned; otherwise, false</returns>
procedure GetEnumPropertyValueFromJObjectByName(PropertyName: Text; var Value: Option): Boolean
begin
exit(JsonImpl.GetEnumPropertyValueFromJObjectByName(PropertyName, Value));
end;

/// <summary>
/// Gets the boolean value at the specified property name in the JSON object.
/// </summary>
/// <param name="PropertyName">The property name</param>
/// <param name="Value">The value</param>
/// <returns>True if the value is returned; otherwise, false</returns>
procedure GetBoolPropertyValueFromJObjectByName(PropertyName: Text; var Value: Boolean): Boolean
begin
exit(JsonImpl.GetBoolPropertyValueFromJObjectByName(PropertyName, Value));
end;

/// <summary>
/// Gets the decimal value at the specified property name in the JSON object.
/// </summary>
/// <param name="PropertyName">The property name</param>
/// <param name="Value">The value</param>
/// <returns>True if the value is returned; otherwise, false</returns>
procedure GetDecimalPropertyValueFromJObjectByName(PropertyName: Text; var Value: Decimal): Boolean
begin
exit(JsonImpl.GetDecimalPropertyValueFromJObjectByName(PropertyName, Value));
end;

/// <summary>
/// Gets the integer value at the specified property name in the JSON object.
/// </summary>
/// <param name="PropertyName">The property name</param>
/// <param name="Value">The value</param>
/// <returns>True if the value is returned; otherwise, false</returns>
procedure GetIntegerPropertyValueFromJObjectByName(PropertyName: Text; var Value: Integer): Boolean
begin
exit(JsonImpl.GetIntegerPropertyValueFromJObjectByName(PropertyName, Value));
end;

/// <summary>
/// Gets the Guid value at the specified property name in the JSON object.
/// </summary>
/// <param name="PropertyName">The property name</param>
/// <param name="Value">The value</param>
/// <returns>True if the value is returned; otherwise, false</returns>
procedure GetGuidPropertyValueFromJObjectByName(PropertyName: Text; var Value: Guid): Boolean
begin
exit(JsonImpl.GetGuidPropertyValueFromJObjectByName(PropertyName, Value));
end;

/// <summary>
/// Replace or add the specified property in the JSON object.
/// </summary>
/// <param name="PropertyName">The property name</param>
/// <param name="Value">The value</param>
/// <returns>True if the property is replaced or added; otherwise, false</returns>
procedure ReplaceOrAddJPropertyInJObject(PropertyName: Text; Value: Variant): Boolean
begin
exit(JsonImpl.ReplaceOrAddJPropertyInJObject(PropertyName, Value));
end;

/// <summary>
/// Add the the JSON object to the JSON array.
/// </summary>
/// <param name="Value">The JSON object in text format</param>
/// <returns>True if the JSON object is added; otherwise, false</returns>
procedure AddJObjectToCollection(Value: Text): Boolean
begin
exit(JsonImpl.AddJObjectToCollection(Value));
end;

/// <summary>
/// Remove the JSON object at the specified index in the JSON array.
/// </summary>
/// <param name="Index">The index of the JSON object</param>
/// <returns>True if the JSON object is removed; otherwise, false</returns>
procedure RemoveJObjectFromCollection(Index: Integer): Boolean
begin
exit(JsonImpl.RemoveJObjectFromCollection(Index));
end;

/// <summary>
/// Replace the specified JSON object in the JSON array.
/// </summary>
/// <param name="Index">The index of the JSON object</param>
/// <param name="Value">The JSON object in text format</param>
/// <returns>True if the JSON object is replaced; otherwise, false</returns>
procedure ReplaceJObjectInCollection(Index: Integer; Value: Text): Boolean
begin
exit(JsonImpl.ReplaceJObjectInCollection(Index, Value));
end;

}
Loading

0 comments on commit 858a482

Please sign in to comment.