Skip to content

Commit a65ffe3

Browse files
author
GlennGeelen
committed
Fix relationships keyword
Fix the included list to return the right objects
1 parent 774f982 commit a65ffe3

1 file changed

Lines changed: 38 additions & 54 deletions

File tree

JSONAPI/Json/JsonApiFormatter.cs

Lines changed: 38 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ protected void Serialize(object value, Stream writeStream, JsonWriter writer, Js
217217
// Now do other stuff
218218
if (relationshipModelProperties.Count() > 0)
219219
{
220-
writer.WritePropertyName("relastionships");
220+
writer.WritePropertyName("relationships");
221221
writer.WriteStartObject();
222222
}
223223
foreach (var relationshipModelProperty in relationshipModelProperties)
@@ -389,70 +389,54 @@ protected void SerializeLinkedResources(Stream writeStream, JsonWriter writer, J
389389
/* Oh, and we have to keep a reference to the TextWriter of the JsonWriter
390390
* because there's no member to get it back out again. ?!?
391391
* */
392-
Dictionary<Type, KeyValuePair<JsonWriter, StringWriter>> writers = new Dictionary<Type, KeyValuePair<JsonWriter, StringWriter>>();
393392

394393
int numAdditions;
395-
do
394+
395+
if (aggregator.Appendices.Count > 0)
396396
{
397-
numAdditions = 0;
398-
Dictionary<Type, ISet<object>> appxs = new Dictionary<Type, ISet<object>>(aggregator.Appendices); // shallow clone, in case we add a new type during enumeration!
399-
foreach (KeyValuePair<Type, ISet<object>> apair in appxs)
397+
writer.WritePropertyName("included");
398+
writer.WriteStartArray();
399+
400+
do
400401
{
401-
Type type = apair.Key;
402-
ISet<object> appendix = apair.Value;
403-
JsonWriter jw;
404-
if (writers.ContainsKey(type))
402+
//// Okay, we should have captured everything now. Now combine the type writers into the main writer...
403+
//foreach (KeyValuePair<Type, KeyValuePair<JsonWriter, StringWriter>> apair in writers)
404+
//{
405+
// apair.Value.Key.WriteEnd(); // close off the array
406+
// writer.WritePropertyName(_modelManager.GetResourceTypeNameForType(apair.Key));
407+
// writer.WriteRawValue(apair.Value.Value.ToString()); // write the contents of the type JsonWriter's StringWriter to the main JsonWriter
408+
//}
409+
numAdditions = 0;
410+
Dictionary<Type, ISet<object>> appxs = new Dictionary<Type, ISet<object>>(aggregator.Appendices); // shallow clone, in case we add a new type during enumeration!
411+
foreach (KeyValuePair<Type, ISet<object>> apair in appxs)
405412
{
406-
jw = writers[type].Key;
407-
}
408-
else
409-
{
410-
// Setup and start the writer for this type...
411-
StringWriter sw = new StringWriter();
412-
jw = new JsonTextWriter(sw);
413-
writers[type] = new KeyValuePair<JsonWriter, StringWriter>(jw, sw);
414-
jw.WriteStartArray();
415-
}
413+
Type type = apair.Key;
414+
ISet<object> appendix = apair.Value;
416415

417-
HashSet<object> tbp;
418-
if (processed.ContainsKey(type))
419-
{
420-
toBeProcessed[type] = tbp = new HashSet<object>(appendix.Except(processed[type]));
421-
}
422-
else
423-
{
424-
toBeProcessed[type] = tbp = new HashSet<object>(appendix);
425-
processed[type] = new HashSet<object>();
426-
}
416+
HashSet<object> tbp;
417+
if (processed.ContainsKey(type))
418+
{
419+
toBeProcessed[type] = tbp = new HashSet<object>(appendix.Except(processed[type]));
420+
}
421+
else
422+
{
423+
toBeProcessed[type] = tbp = new HashSet<object>(appendix);
424+
processed[type] = new HashSet<object>();
425+
}
427426

428-
if (tbp.Count > 0)
429-
{
430-
numAdditions += tbp.Count;
431-
foreach (object obj in tbp)
427+
if (tbp.Count > 0)
432428
{
433-
Serialize(obj, writeStream, jw, serializer, aggregator); // Note, not writer, but jw--we write each type to its own JsonWriter and combine them later.
429+
numAdditions += tbp.Count;
430+
foreach (object obj in tbp)
431+
{
432+
Serialize(obj, writeStream, writer, serializer, aggregator); // Note, not writer, but jw--we write each type to its own JsonWriter and combine them later.
433+
}
434+
processed[type].UnionWith(tbp);
434435
}
435-
processed[type].UnionWith(tbp);
436436
}
437+
} while (numAdditions > 0);
437438

438-
//TODO: Add traversal depth limit??
439-
}
440-
} while (numAdditions > 0);
441-
442-
if (aggregator.Appendices.Count > 0)
443-
{
444-
writer.WritePropertyName("included");
445-
writer.WriteStartObject();
446-
447-
// Okay, we should have captured everything now. Now combine the type writers into the main writer...
448-
foreach (KeyValuePair<Type, KeyValuePair<JsonWriter, StringWriter>> apair in writers)
449-
{
450-
apair.Value.Key.WriteEnd(); // close off the array
451-
writer.WritePropertyName(_modelManager.GetResourceTypeNameForType(apair.Key));
452-
writer.WriteRawValue(apair.Value.Value.ToString()); // write the contents of the type JsonWriter's StringWriter to the main JsonWriter
453-
}
454-
455-
writer.WriteEndObject();
439+
writer.WriteEndArray();
456440
}
457441

458442

0 commit comments

Comments
 (0)