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

[QUESTION] Thread-safety and analogues of jackson features #3323

Open
blonded04 opened this issue Feb 4, 2025 · 3 comments
Open

[QUESTION] Thread-safety and analogues of jackson features #3323

blonded04 opened this issue Feb 4, 2025 · 3 comments
Labels
question Further information is requested

Comments

@blonded04
Copy link

Questions

Hello, I'm struggling with documentation in Chinese language, please help.

I'm trying to migrate from jackson to fastjson2, and I'm stuck with a few questions I can't find answers to:

  1. Can you please tell me, whether fastjson2 is threadsafe (may I serialize different objects from different thrads via JSON / JSONObject static methods?), and if it's not, how can I achieve thread-safety?
  2. I'm trying to migrate from jackson to fastjson2. Are there (if any) direct equivalents of the following functions from jackson in fastjson2:
  • jackson's ObjectReader.readerForUpdating(b): where I provide a builder, and then parse new json's values with respect to values already present in that builder
  • jackson's JsonNode: I'd like to have something like JSONObject, which might be either JSONArray or JSONObject, but I can't find a way to differ one from another, like, if I just have a String and I want to get either a single JSONObject or an array of JSONObjects
@blonded04 blonded04 added the question Further information is requested label Feb 4, 2025
@wenshao
Copy link
Member

wenshao commented Feb 4, 2025

Thank you for choosing fastjson2, I hope it helps you.

  1. The static methods provided by the JSON class are thread-safe, you don't have to worry about that.

  2. Do you think this is what you want?

public class Int1 {
    public int v0000;
}

String str = "{\"v0000\":101}";
Int1 vo = new Int1();
JSONReader jsonReader = JSONReader.of(str);
jsonReader.readObject(vo);
assertEquals(101, vo.getV0000());

@blonded04
Copy link
Author

Thank you for choosing fastjson2, I hope it helps you.

  1. The static methods provided by the JSON class are thread-safe, you don't have to worry about that.
  2. Do you think this is what you want?
public class Int1 {
    public int v0000;
}

String str = "{\"v0000\":101}";
Int1 vo = new Int1();
JSONReader jsonReader = JSONReader.of(str);
jsonReader.readObject(vo);
assertEquals(101, vo.getV0000());

Thank you for your quick answer!

  1. Great!
  2. Is it true, that str may not have all the fields of Int1? May it have only REQUIRED or even a subset of REQUIRED fields? Also I really need some kind of common interface between JSONArray and JSONObject to dynamically decide whether underlying JSO is an array or a simple object

@wenshao
Copy link
Member

wenshao commented Feb 4, 2025

str may not include any Int1 fields. If there are extra fields, they will be ignored. If you want to process extra fields, you can configure ExtraProcessor, for example:

        String str = "{\"v0000\":101,\"name\":\"xx\"}";
        Int1 vo = new Int1();
        JSONReader.Context context = JSONFactory.createReadContext();
        context.setExtraProcessor(((object, key, value) -> {
            // process extra fields
        }));
        JSONReader jsonReader = JSONReader.of(str, context);
        jsonReader.readObject(vo);
        assertEquals(101, vo.getV0000());

JSONObject and JSONArray have no common interface.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants