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

feat: added json library in JSONExceptionWriter #1584

Closed

Conversation

Anirudhrawat
Copy link

rewrite the *JsonExceptionWriter classes to use a JSON library instead of building strings directly.

Description of changes:
Rewrite the *JsonExceptionWriter classes to use a System.Text.JSON library instead of building strings directly.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

rewrite the *JsonExceptionWriter classes to use a JSON library instead of building strings directly.
@ashishdhingra ashishdhingra added module/lambda-client-lib p2 This is a standard priority issue labels Sep 15, 2023
n >>= 4;
}
return hex;
return escapedString.Trim('"');
Copy link
Contributor

Choose a reason for hiding this comment

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

We should call .Trim() too here for consistency

case '\n':
sb.Append(@"\n");
break;
string escapedString = JsonSerializer.Serialize(str);
Copy link
Contributor

Choose a reason for hiding this comment

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

I was testing the output of this method with the one that already exists. They seem to differ.
For instance, with System.Text.Json, we use \u0027 to represent a single quote '. With the original code, we don't.

Input example which leads to different output results: " {\r\n _id: '{{objectId()}}',\r\n index: '{{index()}}',\r\n guid: '{{guid()}}',\r\n isActive: '{{bool()}}',\r\n balance: '{{floating(1000, 4000, 2, \"$0,0.00\")}}',\r\n picture: 'http://placehold.it/32x32',\r\n age: '{{integer(20, 40)}}',\r\n eyeColor: '{{random(\"blue\", \"brown\", \"green\")}}',\r\n name: '{{firstName()}} {{surname()}}',\r\n gender: '{{gender()}}',\r\n company: '{{company().toUpperCase()}}',\r\n email: '{{email()}}',\r\n phone: '+1 {{phone()}}',\r\n address: '{{integer(100, 999)}} {{street()}}, {{city()}}, {{state()}}, {{integer(100, 10000)}}',\r\n about: '{{lorem(1, \"paragraphs\")}}',\r\n registered: '{{date(new Date(2014, 0, 1), new Date(), \"YYYY-MM-ddThh:mm:ss Z\")}}',\r\n latitude: '{{floating(-90.000001, 90)}}',\r\n longitude: '{{floating(-180.000001, 180)}}',\r\n tags: [\r\n '{{repeat(7)}}',\r\n '{{lorem(1, \"words\")}}'\r\n ], "

Copy link
Contributor

Choose a reason for hiding this comment

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

However, if we use LitJson. Code example below:

// Create a JsonData object to hold the string
JsonData jsonData = new JsonData(str);

// Serialize the JsonData object to a JSON string
string litjsonString = JsonMapper.ToJson(jsonData);
litjsonString = litjsonString.Trim('"').Trim();

We get the same result

@@ -22,96 +22,16 @@ internal class JsonExceptionWriterHelpers
/// <summary>
/// This method escapes a string for use as a JSON string value.
/// It was adapted from the PutString method in the LitJson.JsonWriter class.
Copy link
Contributor

Choose a reason for hiding this comment

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

Comment on line 24 should be deleted

@@ -13,7 +13,7 @@
* permissions and limitations under the License.
*/

using System.Text;
using System.Text.Json;
Copy link
Contributor

Choose a reason for hiding this comment

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

It seems like in this commit, we migrated from LitJSON to System.Text.Json and left this method as is. I am not sure why though.

Copy link
Contributor

@peterrsongg peterrsongg left a comment

Choose a reason for hiding this comment

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

To echo @CamileDahdah's point, I just wrote a unit test that tests the functionality of just the cases where c > ' ' so basically everything except the default. Looks like your implementation does not work for the escaped quotation. At a minimum the PR for this should include a unit test that verifies that your implementation works. Can you please include a unit test in the next iteration? Thank you

    public class UnitTest1
    {
 
        private static string convertCharacterStrings = "\n\r\t\"\\\f\b\u0085\u2028\u2029";
        [Fact]
        public void TestEscapeStringForJsonReturnsSameOutputAsSystemTextJson()
        {
            string escapeStringForJsonCharacter;
            string systemTextJsonCharacter;
            escapeStringForJsonCharacter = EscapeStringForJson(convertCharacterStrings);
            systemTextJsonCharacter = JsonSerializer.Serialize(convertCharacterStrings).Trim('"');

            Assert.Equal(escapeStringForJsonCharacter, systemTextJsonCharacter);
        }
}

@peterrsongg
Copy link
Contributor

Thank you for the PR but we can't take a dependency on LitJson. In the SDK we have a source-copy of LitJson, so I can see why you added the dependency but for this library we don't even have a source-copy in this library and we don't want to add another dependency. Going to close out this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
module/lambda-client-lib p2 This is a standard priority issue
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants