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

Conversation

DmitryKatson
Copy link
Contributor

@DmitryKatson DmitryKatson commented Mar 11, 2024

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 theBase Application codeunit 5459 "Json management"

Summary

This PR is required for the Number Series Copilot implementation as we agreed here

Work Item(s)

Fixes #715

Fixes AB#506714

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 partly ported from Base Application codeunit 5459 "Json management"
@DmitryKatson DmitryKatson requested a review from a team as a code owner March 11, 2024 07:02
@github-actions github-actions bot added AL: System Application From Fork Pull request is coming from a fork labels Mar 11, 2024
This was referenced Mar 11, 2024
@DmitryKatson
Copy link
Contributor Author

@microsoft-github-policy-service agree

@JesperSchulz JesperSchulz linked an issue Mar 11, 2024 that may be closed by this pull request
1 task
Copy link
Contributor

@JesperSchulz JesperSchulz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests missing before code review can continue.

Added app.json file defining a JSON module and its dependencies, as well as a new codeunit "Json Test" containing tests for JSON functionality, specifically around JSON collection manipulation and value extraction. This enhances the system's testing capabilities around JSON processing.
Add individual functions to retrieve various data types from JSON objects, like strings, boolean, integer, decimals etc.
@JesperSchulz
Copy link
Contributor

I'm generally happy with this module! Clean API, well tested!

If you get rid of the last two codecop issues which prevent compilation, I'll get another set of eyes on this and in it goes.

@JesperSchulz JesperSchulz added the Linked Issue is linked to a Azure Boards work item label Mar 13, 2024
…bles order declaration

The GetObjectFromCollectionByIndex method signature has been modified to accept the index parameter before the JSON object in text format parameter. This change aims to improve consistency and adherence to best practices.
@DmitryKatson
Copy link
Contributor Author

I'm generally happy with this module! Clean API, well tested!

If you get rid of the last two codecop issues which prevent compilation, I'll get another set of eyes on this and in it goes.

Pushed commit with fixes. Please take a look

…pulate JSON arrays

Added functions to modify, add, or remove properties, as well as manipulate JSON arrays. Extracted JSON array and object retrieval methods to improve code readability and maintainability. These changes enhance the flexibility and reusability of JSON handling.
@DmitryKatson
Copy link
Contributor Author

New commit 354ecd2 is pushed with the fixes on the provided comments and also new apis, together with new tests
image

@JesperSchulz
Copy link
Contributor

@duichwer, are you happy with this PR now? If you are, I'll do some manual testing and will do a detailed review, and then we're good to go here, I'd say.

@duichwer
Copy link

In general I'm Okay with this PR for now.
Will it be possible to have an overload for the procedure GetObject in the future that returns the native AL JsonObject in the future?

I'm never sure which overloads are possible in AL.
Same applies for the GetCollection procedure.

If this isn't possible then I'd suggest to rename those procedures to something like GetObjectAsText

Besides that I'm totally fine with this PR.

@JesperSchulz
Copy link
Contributor

Two more build issues:
Error: AA0137 Variable 'IsHandled' is unused in 'AddJPropertyToJObject'.
Error: AA0137 Variable 'JArray' is unused in 'AddJObjectToCollection'.

We're getting there :)

@JesperSchulz
Copy link
Contributor

In general I'm Okay with this PR for now. Will it be possible to have an overload for the procedure GetObject in the future that returns the native AL JsonObject in the future?

I'm never sure which overloads are possible in AL. Same applies for the GetCollection procedure.

If this isn't possible then I'd suggest to rename those procedures to something like GetObjectAsText

Besides that I'm totally fine with this PR.

You cannot overload on the return type alone. The parameters must be different. Why not go with the "AsText" suggestion? It makes it very clear in which shape you'll get the object / collection. I'd vote for that.

Refactor JSON codeunit methods to return JSON array and object directly as well as in text format, enhancing flexibility and usability. Update related test cases accordingly.
@DmitryKatson
Copy link
Contributor Author

In general I'm Okay with this PR for now. Will it be possible to have an overload for the procedure GetObject in the future that returns the native AL JsonObject in the future?
I'm never sure which overloads are possible in AL. Same applies for the GetCollection procedure.
If this isn't possible then I'd suggest to rename those procedures to something like GetObjectAsText
Besides that I'm totally fine with this PR.

You cannot overload on the return type alone. The parameters must be different. Why not go with the "AsText" suggestion? It makes it very clear in which shape you'll get the object / collection. I'd vote for that.

It's done.
previous GetCollection and GetObject renamed to GetCollectionAsText and GetObjectAsText accordinly. New GetCollection and GetObject added with JsonArray and JsonObject return values.

Tests updated. Please check
image

@grobyns
Copy link
Contributor

grobyns commented Mar 19, 2024

We've got these enabled by default:

"al.codeAnalyzers":  [
                         "${AppSourceCop}",
                         "${CodeCop}"
                     ],

We also have PTECop and UICop enabled but I think the thing you need is to use the same ruleset as we do:
the GitHub AL:Go! setup looks like this:

"enableCodeCop":  true,
"enableAppSourceCop":  true,
"enablePerTenantExtensionCop":  true,
"enableUICop":  true,
"enableCodeAnalyzersOnTestApps":  true,
"rulesetFile":  "..\\..\\..\\src\\rulesets\\ruleset.json",

@JesperSchulz
Copy link
Contributor

JesperSchulz commented Mar 19, 2024

We've got these enabled by default:

"al.codeAnalyzers":  [
                         "${AppSourceCop}",
                         "${CodeCop}"
                     ],

We also have PTECop and UICop enabled but I think the thing you need is to use the same ruleset as we do: the GitHub AL:Go! setup looks like this:

"enableCodeCop":  true,
"enableAppSourceCop":  true,
"enablePerTenantExtensionCop":  true,
"enableUICop":  true,
"enableCodeAnalyzersOnTestApps":  true,
"rulesetFile":  "..\\..\\..\\src\\rulesets\\ruleset.json",

My apologies. When I was looking into the settings file, I was working on the BaseApp. Addendum: The ruleset used here is base.ruleset.json, which I don't know if we're actually shipping (will find out).

For the System App and Business Foundation, we've indeed got all 4 cops enabled with the internal.module.ruleset.json when compiling modules individually, and the ruleset.json when compiling the entire layer into an app.

This I indeed should document :-)

@JesperSchulz JesperSchulz changed the title Add Json functionality for working with JSON data [Copilot No. Series] Add Json functionality for working with JSON data Mar 20, 2024
Simplify and restructure JSON parsing methods to enhance readability and provide greater flexibility for handling JSON data.
These changes also aim to improve maintainability and extensibility of the codebase. No related issues.
JesperSchulz
JesperSchulz previously approved these changes Mar 22, 2024
Copy link
Contributor

@JesperSchulz JesperSchulz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks fine to me now. Approved!

DmitryKatson and others added 2 commits March 22, 2024 19:24
Consolidate variable declaration to enhance code readability and maintainability.
This change eliminates redundancy and streamlines the code structure.
Apply namespace changes

Co-authored-by: Darrick <[email protected]>
JesperSchulz
JesperSchulz previously approved these changes Mar 22, 2024
darjoo
darjoo previously approved these changes Mar 22, 2024
@JesperSchulz JesperSchulz enabled auto-merge (squash) March 22, 2024 13:31
@github-actions github-actions bot added this to the Version 25.0 milestone Mar 22, 2024
@JesperSchulz JesperSchulz dismissed stale reviews from darjoo and themself via b9d9d74 March 22, 2024 13:50
@JesperSchulz JesperSchulz merged commit 858a482 into microsoft:main Mar 22, 2024
23 checks passed
@m3koenig
Copy link

I like this one! Are there usage examples avaibalbe anywhere (asides from the Tests :D)?
Would be helpful.

@DmitryKatson
Copy link
Contributor Author

I like this one! Are there usage examples avaibalbe anywhere (asides from the Tests :D)? Would be helpful.

You can find some additional examples here or just search for :Json: Codeunit Json;. Later, when this be ready, it will be merged to master

@JesperSchulz JesperSchulz added the Integration GitHub request for Integration area label May 16, 2024
@DmitryKatson DmitryKatson deleted the Json-Mgt-Impl branch September 14, 2024 05:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
AL: System Application From Fork Pull request is coming from a fork Integration GitHub request for Integration area Linked Issue is linked to a Azure Boards work item
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BC Idea]: Json Module
7 participants