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

Utility Method for Extracting JSON from Mixed-Content Strings #2772

Open
Mohit1r opened this issue Oct 31, 2024 · 1 comment
Open

Utility Method for Extracting JSON from Mixed-Content Strings #2772

Mohit1r opened this issue Oct 31, 2024 · 1 comment

Comments

@Mohit1r
Copy link

Mohit1r commented Oct 31, 2024

Problem solved by the feature

The requested feature addresses the common scenario where JSON data is embedded within non-JSON text, such as log messages, API responses, or concatenated strings. Currently, developers must write custom parsing logic to extract this JSON data, which can lead to code duplication and increased complexity. This method would streamline the process, allowing developers to easily extract JSON fragments and work with them without additional overhead.

Feature description

The proposed feature is a utility method named extractJsonFromString(String input, String startToken, String endToken). This method will locate and extract JSON content from a larger string based on specified delimiters (e.g., { and }). It will handle nested JSON structures and provide error handling for malformed or missing JSON data. By simplifying the extraction process, this feature will enhance the usability of Gson for applications dealing with mixed-content strings.

Alternatives / workarounds

Currently, developers often resort to using regular expressions or manual string manipulation to extract JSON from embedded text. These methods can be error-prone, especially when dealing with nested structures or varying formats. Some may use third-party libraries for JSON parsing or custom utility methods, but these approaches add unnecessary dependencies and complexity. The proposed method would offer a standardized solution within the Gson library, reducing the need for such workarounds.

@Marcono1234
Copy link
Collaborator

Marcono1234 commented Nov 2, 2024

Thanks for the suggestion! I am not completely sure if Gson should really offer such a functionality. Users can probably implement this in a more flexible way and without that much code themselves. Probably something similar to this:

String str = ...;
int jsonStart = str.indexOf("{");  // or depending on use case more advanced detection, such as regex search
JsonReader jsonReader = new JsonReader(str.substring(jsonStart));
jsonReader.setStrictness(Strictness.STRICT);
MyClass deserialized = new Gson().fromJson(jsonReader, MyClass.class);

The main problem with this might be that you don't know where the JSON substring ended because JsonReader performs internal buffering, see also #1180.

(This is my personal opinion on this, I am not a direct member of this project.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants