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

Can I create a DataModel object from a detached JSON string? #9

Open
deanebarker opened this issue Sep 3, 2016 · 3 comments
Open

Comments

@deanebarker
Copy link

Is it possible to create one of your DataModel objects from a JSON string?

I wrote a webhook server for Contentful. Each webhook request sends an entire object as its request body in JSON. I'm looking for a way to get an object representation of this.

Using that JSON string, could I create one of your DataModel objects?

@ryan-codingintrigue
Copy link
Owner

You should be able to do this. Contentful.NET uses JSON.NET to deserialize the response from the REST API to DataModel objects, so if you use the same library it should work fine.

@deanebarker
Copy link
Author

Do I do it manually, or can I just pass the string somewhere? I really don't want to re-implement the string-to-object logic as I don't think that would do either library any favors.

@ryan-codingintrigue
Copy link
Owner

The method used by Contentful.NET is internal so you won't be able to call it directly (it requires a HttpResponseResult anyway, so probably won't be of much use to you).

The simplest method would be JsonConvert.DeserializeObject:

var yourObject = JsonConvert.DeserializeObject<T>(yourJsonString);

Where T is any implementation of IContentfulItem - which covers all of the top-level DataModels.

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

No branches or pull requests

2 participants