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

Exposing two new APIs trySerialize() and tryDeserialize() which do not fail fast on any error occurrence. Instead, they return maximum possible serializable/deserializable object/array and all errors. #103

Open
wants to merge 9 commits into
base: master
Choose a base branch
from

Conversation

kevi793
Copy link

@kevi793 kevi793 commented Jun 4, 2019

Added two new APIs:
(a) trySerialize<T>(data: T | T[]): {value: any | any[], error: any} { ... }
This method tries to serialize input data and returns value: maximum possible serializable array/object and error: errors found in serialization.
(b) tryDeserialize<T>(json: any, classReference: { new(): T }): {value: any, error: any} { ... }
This method tries to deserialize input json and returns value: maximum possible deserializable array/object and error: errors found in deserialization.

Example usage:-
Snake & Human Class:-

class Snake {
    @JsonProperty("snakeName", String)
    name: string = "";

    @JsonProperty()
    district: number = 0;  

    @JsonProperty("owner", Human)
    owner: Human = undefined;
}

class Human {
    @JsonProperty("givenName", String)
    firstname: string = "";

    @JsonProperty("lastName", String)
    lastname: string = "";
}

Input Json to be deserialized to Snake Type:-

{
	"district": 20,
	  "owner": {
	  	"firstName": "Harry"
	  }
}
{value, error} = jsonconvert.TryDeserialize<Snake>(inputJson, Snake);

Error Object Returned :-

{
	"name": "Property is missing"
  	"owner": {
		"lastname": "Property is missing"
	}
}

Value Object Returned :-

  {
	"name": "",
  	"owner": {
		"firstName": "Harry",
  		"lastname": "Property is missing"
	}
}

@kevi793 kevi793 changed the title Kevi793/try validate api Exposing two new APIs trySerialize() and tryDeserialize() which do not fail fast on any error occurrence. And returns maximum possible serializable/deserializable object/array and errors Jun 4, 2019
@kevi793 kevi793 changed the title Exposing two new APIs trySerialize() and tryDeserialize() which do not fail fast on any error occurrence. And returns maximum possible serializable/deserializable object/array and errors Exposing two new APIs trySerialize() and tryDeserialize() which do not fail fast on any error occurrence. Instead, they return maximum possible serializable/deserializable object/array and all errors. Jun 4, 2019
@andreas-aeschlimann andreas-aeschlimann self-assigned this Jun 4, 2019
@andreas-aeschlimann
Copy link
Member

Thank you for this interesting approach.

It's a good idea to add a method to try deserialization and serialization. I didn't have time yet to have a deep look. How do you create an instance of a class partially?

I think the value object returned should be rather

{
	"name": "",
  	"owner": {
		"firstName": "Harry",
  		"lastname": ""
	}
}

(if the property was missing in the JSON, use the default value).

What was your motivation to change the return values of the existing deserialize/serialize methods? I don't think we should change them because this will mean a breaking change for all developers using the library.

@kevi793
Copy link
Author

kevi793 commented Jun 4, 2019

Thanks for immediate feedback on the PR :)

(1) What was your motivation to change the return values of the existing deserialize/serialize methods?

I have changed the return values of only serializeObject()/serializeArray() and deserializeObject()/deserializeArray() methods. The wrapper methods, serialize() & deserialize(), are untouched. Their functionality remains the same. My assumption was that any user would use wrapper methods rather than specific methods for array or object.Now after looking at the documentation examples, I realise that users might be using specific deserialize/serialize methods depending on array or objects.

(2) How do you create an instance of a class partially?

(a) In case of serialization, the *value* will have all valid properties. Missing/Invalid properties will not be present. 
(b) In case of deserialization, corresponding missing/invalid properties in *value* will have default value for that type. 

Looking forward to your code review. I will be happy to contribute if you ask me to address anything or may be changing the implementation to take into account your above mentioned concerns :)

If you feel that we should have this kind of methods (trySerialize() and tryDeserialize()) with all errors, then please let me know.

…isting developer experience does not break
@kevi793
Copy link
Author

kevi793 commented Jun 5, 2019

Hello Andreas. Updated the PR to retain existing serialize/deserialize methods. Now, there is no breaking experience.

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