Getting exception "Parsing JSON feeds or entries without model is not supported."
-
2012년 8월 6일 월요일 오후 9:10
Hi all;
I'm following the instructions in the code included with http://blogs.msdn.com/b/astoriateam/archive/2011/10/14/introducing-the-odata-library.aspx and using a format of "application/json". I am trying to read the response from: "http://services.odata.org/OData/OData.svc/Categories(1)"
I am getting the exception:
Microsoft.Data.OData.ODataException occurred Message="Parsing JSON feeds or entries without model is not supported." Source="Microsoft.Data.OData" StackTrace: at Microsoft.Data.OData.Json.ODataJsonReader..ctor(ODataJsonInputContext jsonInputContext, IEdmEntityType expectedEntityType, Boolean readingFeed, IODataReaderWriterListener listener) in C:\src\jenova\Dev\Merge\ODataLib\OData\Orcas\Microsoft\Data\OData\Json\ODataJsonReader.cs:line 55So I then tried "application/atom+xml" and got:
Microsoft.Data.OData.ODataException occurred Message="An element with name 'entry' in namespace 'http://www.w3.org/2005/Atom' was found; however, a feed was expected. A feed must be represented as a {http://www.w3.org/2005/Atom}:feed element." Source="Microsoft.Data.OData" StackTrace: at Microsoft.Data.OData.Atom.ODataAtomEntryAndFeedDeserializer.ReadFeedStart() in C:\src\jenova\Dev\Merge\ODataLib\OData\Orcas\Microsoft\Data\OData\Atom\ODataAtomEntryAndFeedDeserializer.cs:line 388Any idea why? And how can I read data from an OData datasource?
thanks - dave
Who will win The International Collegiate Programming Championships?
모든 응답
-
2012년 8월 6일 월요일 오후 10:53중재자
For reading json, OData library requires the model. Here's the code snippet which shows how to get the model instance:
private static IEdmModel GetModel() { var webRequest = (HttpWebRequest)HttpWebRequest.Create("http://services.odata.org/Northwind/Northwind.svc/$metadata"); webRequest.Method = "GET"; var response = webRequest.GetResponse(); using (XmlReader xmlReader = XmlReader.Create(response.GetResponseStream())) { IEdmModel model; IEnumerable<EdmError> errors; if (EdmxReader.TryParse(xmlReader, out model, out errors)) { return model; } throw new Exception("Error parsing edm model"); } }Hope this helps.
Thanks
Pratik
This posting is provided "AS IS" with no warranties, and confers no rights.
- 답변으로 표시됨 DavidThi808 2012년 8월 7일 화요일 오후 4:51
-
2012년 8월 6일 월요일 오후 11:02
Hi Pratik;
Ok, thank you. I missed that.
Dump question (of mine) - since the service knows its metadata, why does it have to be passed?
thanks - dave
Who will win The International Collegiate Programming Championships?
-
2012년 8월 7일 화요일 오전 6:59중재자
Hi,
The service does know the model, but the client doesn't. The ODataLib on the client needs to have the model in order to correctly read the JSON payload. That's one of the differences between the JSON and ATOM formats. ATOM can mostly be read without knowing the model, since it has lot of additional information it. JSON is much more terse and thus sometimes ambiguous without a model. So the reader requires a model in order to disambiguate some cases. Even ATOM can't read everything correctly without a model. If the service uses Entity Property Mapping feature, the ATOM reader requires the model as well. For example the feed returned by http://services.odata.org/OData/OData.svc/Products uses EPM (this is visible in the $metadata where it has m:FC_TargetPath attributes on the Product type, and is also visible in the payload since the ATOM title element has some real value in it, without EPM the title element would be empty). So in general we strongly recommend to always pass in the model, no matter what format you're reading or writing. (The support for reading ATOM without model is targeted for some specific scenarios where the model is not readily available, but it's definitely not a typical use case).
As to your error when reading the ATOM payload, this is because you're reading a single entry response as if it was a feed. ~/Categories(1) will return a single entry (in ATOM you can see this by looking at the root element of the response, it will be entry). So to read this you need to use the ODataMessageReader.CreateODataEntryReader method. It does return the same ODataReader as if you would call CreateODataFeedReader, but the state machine internally is different. (It uses the same reader because if the entry has expanded navigation properties it can report feeds and other entries as well, so there was no point having two reader interfaces which would look pretty much identical).
You would run into the same problem even in JSON, once you fix the model requirement.
Thanks,
Vitek Karas [MSFT]
- 답변으로 표시됨 DavidThi808 2012년 8월 7일 화요일 오후 4:51
-
2012년 8월 7일 화요일 오후 4:44
Ok, further but still a problem. I'm reading and using the model. And now on my first call to reader.Read() I am getting:
Microsoft.Data.OData.ODataException occurred Message="Did not find the required 'results' property on the object wrapping a feed." Source="Microsoft.Data.OData" StackTrace: at Microsoft.Data.OData.Json.ODataJsonEntryAndFeedDeserializer.ReadFeedStart(ODataFeed feed, Boolean isResultsWrapperExpected, Boolean isExpandedLinkContent) in C:\src\jenova\Dev\Merge\ODataLib\OData\Orcas\Microsoft\Data\OData\Json\ODataJsonEntryAndFeedDeserializer.cs:line 81On the url "http://services.odata.org/OData/OData.svc/Categories(1)" using a format of "application/json"
If I use a format of "application/atom+xml" I get:
Microsoft.Data.OData.ODataException occurred Message="An element with name 'entry' in namespace 'http://www.w3.org/2005/Atom' was found; however, a feed was expected. A feed must be represented as a {http://www.w3.org/2005/Atom}:feed element." Source="Microsoft.Data.OData" StackTrace: at Microsoft.Data.OData.Atom.ODataAtomEntryAndFeedDeserializer.ReadFeedStart() in C:\src\jenova\Dev\Merge\ODataLib\OData\Orcas\Microsoft\Data\OData\Atom\ODataAtomEntryAndFeedDeserializer.cs:line 388Any idea what I have wrong?
thanks - dave
Who will win The International Collegiate Programming Championships?
-
2012년 8월 7일 화요일 오후 4:51
Found it - needed CreateODataEntryReader instead of CreateODataFeedReaderWho will win The International Collegiate Programming Championships?
- 답변으로 표시됨 DavidThi808 2012년 8월 7일 화요일 오후 4:51

