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

Teeny json suport #3

Merged
merged 18 commits into from
Jul 24, 2024
Merged

Conversation

alex-cova
Copy link
Collaborator

Removed @transient and java.desktop requirement, and created @JsonIgnore as replacement.

Usage:

String json = new TeenyJson().writeValueAsString(person);

Copy link
Owner

@JonathanGiles JonathanGiles left a comment

Choose a reason for hiding this comment

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

Looks pretty good - just needs less TODOs and more JavaDoc :D

@alex-cova
Copy link
Collaborator Author

TeenyJson now can parse more complex JSONs like:

{
  "name": "Alex",
  "age": 25,
  "objectB": {
    "name": "Jonathan",
    "age": 30,
    "objectC": {
      "name": "John Doe",
      "age": 23
    }
  },
  "developer": true
}
ObjectA objectA = new TeenyJson()
                .readValue(json, ObjectA.class);

ObjectA.class

Also collections

List<ProductDto> products = new TeenyJson()
                .readList(json, ProductDto.class);

I should change readList to readCollection(json, List.class, ProductDto.class) to match the user collection.

Comment on lines 121 to 140
String baseName = method.getName().substring(3);

Method accessorMethod;

if (method.getParameterTypes()[0] == boolean.class) {
accessorMethod = methodMap.get("is" + baseName);
} else {
accessorMethod = methodMap.get("get" + baseName);
}

if (accessorMethod != null) {
JsonAlias jsonAlias = accessorMethod.getAnnotation(JsonAlias.class);

if (jsonAlias != null) {
return jsonAlias.value();
}
}

return method.getName().substring(3, 4).toLowerCase()
+ method.getName().substring(4);
Copy link
Owner

Choose a reason for hiding this comment

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

This is largely equivalent to the getFieldName method above. I wonder if we could reduce the repetition?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I moved the repeated code to:

private static String findAlias(Map<String, Method> methodMap, String baseName, Class<?> type) {
        Method accessorMethod;

        if (type == boolean.class) {
            accessorMethod = methodMap.get("is" + baseName);
        } else {
            accessorMethod = methodMap.get("get" + baseName);
        }

        if (accessorMethod != null) {
            JsonAlias jsonAlias = accessorMethod.getAnnotation(JsonAlias.class);

            if (jsonAlias != null) {
                return jsonAlias.value();
            }
        }

        return null;
    }

* @return the parsed object which could be null.
*/
@SuppressWarnings({"rawtypes", "unchecked"})
public <T, K> K readCollection(String json, Class<? extends Collection> collectionType, Class<T> type) {
Copy link
Owner

Choose a reason for hiding this comment

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

Shouldn't the second parameter be Class<K extends Collection> collectionType?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, but this may be the best choice:

public <T, K extends Collection> K readCollection(String json, Class<K> collectionType, Class<T> type)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Had to switch back to

public <T, K> K readCollection(String json, Class<? extends Collection> collectionType, Class<T> type) {

because this:

public <T, K extends Collection> K readCollection(String json, Class<K> collectionType, Class<T> type)

Raises a warning about unchecked assignment 😪

Screenshot 2024-03-07 at 10 01 43 p m

@@ -25,6 +28,7 @@ jobs:
run: mvn --batch-mode -DskipTests package

- name: Test
timeout-minutes: 5
Copy link
Owner

Choose a reason for hiding this comment

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

Nice addition!

@JonathanGiles
Copy link
Owner

Do you want this merged now, or do you want to keep working on it? We also need to update the readme.

@alex-cova
Copy link
Collaborator Author

Do you want this merged now, or do you want to keep working on it? We also need to update the readme.

uff I didn't realize that there are a lot of cases, there is a few cases missing especially with collections an Maps, I believe to have a decent version this week

@JonathanGiles
Copy link
Owner

Let me know when you want another review!

@alex-cova
Copy link
Collaborator Author

Let me know when you want another review!

Sorry for the late response, I've been sick these days. Yes please, another review would be awesome, I consider this PR can now be merged

@JonathanGiles
Copy link
Owner

I'm back! I've been travelling and busy with work / life. I'll get to reviewing this today.

@alex-cova
Copy link
Collaborator Author

I'm back! I've been travelling and busy with work / life. I'll get to reviewing this today.

Welcome back!, no problem I was also quite busy :P looking forward for new challenges

readme.md Outdated Show resolved Hide resolved
readme.md Outdated Show resolved Hide resolved
@JonathanGiles
Copy link
Owner

Only very minor feedback - then we can merge. Thanks

alex-cova and others added 2 commits July 23, 2024 18:52
Co-authored-by: Jonathan Giles <[email protected]>
Co-authored-by: Jonathan Giles <[email protected]>
@JonathanGiles JonathanGiles merged commit 3bfe7d6 into JonathanGiles:master Jul 24, 2024
1 check failed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants