1

I'm writing a recursive method that basically traverses a NSManagedObject object and converts it to a JSON dictionary. I have the bulk of this done, but I am running into an issue where the method goes into an infinite loop when it comes to an object's inverse.

For example, let's say the method starts off with an object that has a class of Job, and inside of that Job object there is a property called surveys. The surveys is an NSSet that contains multiple JobSurvey objects. Each JobSurvey object contains an inverse back to the original Job object and the property is called "Job".

When I run this through my method it starts the infinite loop by going into the job and starts to process each property. Once the method gets to the surveys property, it'll be called again to process each JobSurvey object as expected. The method then processes each property until it reaches the Job (inverse) object. At that time it'll continue to process that object, and thus creates the infinite loop.

Any thoughts on how I can fix this? I'm trying to write this method without having to create custom object classes with object mapping as it needs to be able to be used with any type of object I pass into it. Below is the code I have thus far.

- (NSDictionary *)encodeObjectsForJSON:(id)object
{
    NSMutableDictionary *returnDictionary = [[NSMutableDictionary alloc] init];

    // get the property list for the object
    NSDictionary *props = [VS_PropertyUtilities classPropsFor:[object class]];

    for (NSString *key in props) {

        // get the value for the property from the object
        id value = [object valueForKey:key];

        // if the value is just null, then set a NSNull object
        if (!value) {

            NSNull *nullObj = [[NSNull alloc] init];
            [returnDictionary setObject:nullObj forKey:key];

        // if the value is an array or set, then iterate through the array or set and call this method again to encode it's values
        } else if ([value isKindOfClass:[NSArray class]] || [value isKindOfClass:[NSSet class]]) {

            NSMutableArray *retDicts = [[NSMutableArray alloc] init];

            // encode each member of the array to a JSON dictionary
            for (id val in value) {
                [retDicts addObject:[self encodeObjectsForJSON:val]];
            }

            // add to the return dictionary
            [returnDictionary setObject:retDicts forKey:key];

        // else if this is a foundation object, then set it to the dictionary
        } else if ([value isKindOfClass:[NSString class]] || [value isKindOfClass:[NSNumber class]] || [value isKindOfClass:[NSNull class]] || [value isKindOfClass:[NSDate class]]) {

            [returnDictionary setObject:value forKey:key];

        // else this must be a custom object, so call this method again with the value to try and encode it
        } else {

            NSDictionary *retDict = [self encodeObjectsForJSON:value ];
            [returnDictionary setObject:retDict forKey:key];
        }
    }

    return returnDictionary;
}
5
  • Does the JSON need to represent the inverse explicitly? Oct 3, 2012 at 19:13
  • No...the JSON doesn't need the inverse at all. I've come up with a possible solution, but it's a bit of a pain. I added an NSObject wrapper class and am subclassing that class to explicitly say which properties to ignore during the encoding. However, it would be nice if i could automate the process. Oct 3, 2012 at 19:48
  • At first I thought this was only for NSManagedObjects and you could iterate through the NSRelationshipDescriptions of the entity and pass the name of the inverse in as the property to ignore. Oct 3, 2012 at 19:52
  • Well, really I'm probably only ever going to use it with NSManagedObjects, but since I'm building a library that will be used in a lot of apps I wanted it to be robust enough to be able to accept any type of NSObject and encode it for JSON. That way it'll work if we just want to push some generated data that isn't stored via Core Data to a server. At the EOD I think your solution would work well, and it's less of a hassle use than the solution I've come up with. I'll give it a look. Thanks for the info! Oct 3, 2012 at 20:06
  • Yeah no problem, I've got an idea for a somewhat more general solution that I'll try to post as an answer tonight but don't have time right now :) Oct 3, 2012 at 20:23

1 Answer 1

0

This is what I did. It is similar to what you are trying to achieve.

link to da codes

It is a bit more explicit than i think you are looking for but I thought I'd suggest.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.