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

[BUG] Prettify JSON has wrong space when prettifying an array of number #178

Closed
NghiaTranUIT opened this issue Jul 25, 2024 · 2 comments
Closed
Labels
bug Something isn't working

Comments

@NghiaTranUIT
Copy link

Describe the bug
When prettifying a given JSON, the array of numbers lost their intent space.

Here is my code to prettify a given JSON String. Please note that YYJSON_READ_NUMBER_AS_RAW is important because we need to prettify the large floating number.

+(nullable NSString *) __useYYJSONToPrettifyWithText:(NSString *) text tabWidth:(NSInteger) tabWidth {
    // Convert to c-string
    auto cString = [text cStringUsingEncoding:NSUTF8StringEncoding];
    if (cString == nil) {
        return nil;
    }

    // Read JSON and get root
    yyjson_doc *idoc = yyjson_read(cString, strlen(cString), YYJSON_READ_NUMBER_AS_RAW); // important
    yyjson_mut_doc *doc = yyjson_doc_mut_copy(idoc, NULL);

    // To string, minified
    yyjson_write_flag flg = 0;
    if (tabWidth == 2) {
        flg = YYJSON_WRITE_PRETTY_TWO_SPACES;
    } else {
        flg = YYJSON_WRITE_PRETTY;
    }

    const char *jsonCString = yyjson_mut_write(doc, flg, NULL);
    if (jsonCString != NULL) {

        NSString *result = [NSString stringWithCString:jsonCString encoding:NSUTF8StringEncoding];

        // Free all
        free((void *)jsonCString);
        yyjson_doc_free(idoc);
        yyjson_mut_doc_free(doc);

        // Done
        return result;
    }

    // Free the doc
    yyjson_doc_free(idoc);
    yyjson_mut_doc_free(doc);

    return nil;
}

Input

{"name": "JSON Sample","ids": [1,2,3,4,5,6]}

Current output

{
  "name": "JSON Sample",
  "ids": [
1,
2,
3,
4,
5,
6
  ]
}

Expected output

{
  "name": "JSON Sample",
  "ids": [
    1,
    2,
    3,
    4,
    5,
    6
  ]
}

Your environment

  • OS: macOS 14.5
  • yyjson Version: 0.9.0
@ibireme ibireme added the bug Something isn't working label Jul 25, 2024
@ibireme
Copy link
Owner

ibireme commented Jul 25, 2024

Fixed.

@ibireme ibireme closed this as completed Jul 25, 2024
@NghiaTranUIT
Copy link
Author

thanks so much, it's quick 🥇

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants