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 6 commits
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/"
}
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.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>
/// <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
DmitryKatson marked this conversation as resolved.
Show resolved Hide resolved
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.GetPropertyValueByName(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.GetStringPropertyValueByName(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
Loading