ovidiu
ovidiu

Reputation: 11

couldn't serialize an NSObject, in Json

I'm writing an iPhone app using JSON framework. I couldn't serialize an NSObject, I found only serialize for NSString, NSArray, NSData, but I want to serialize a generic object from template object. Any class library for this?

I have a user class, with userid, username and password. I want to serialize this class and send to server. After I want to deserialize result from server. Result could be class User, or any other class. In c# I have this: (In this case I use JSONRpc)

private int userid;
        [JsonIgnore]
public int Userid
{
   get { return userid; }
   set { userid = value; }
}
private string username;
[JsonProperty("username")]
public string Username
{
    get { return username; }
    set { username = value; }
}
private string password;
[JsonProperty("password")]
public string Password
{
    get { return password; }
    set { password = value; }
}

And method for response is: public JsonRpcResponse Invoke (string method, params object[] args). (T = template, in this case User) I can do this in Objective-C?

Do you know any solution?

Upvotes: 1

Views: 1297

Answers (2)

Mahadevan Sreenivasan
Mahadevan Sreenivasan

Reputation: 1132

You might want to checkout my implementation for serializing NSObjects to JSON and vice versa - https://github.com/QBurst/KVCObjectSerializer

Upvotes: 1

Felixyz
Felixyz

Reputation: 19143

No you can't. You need to turn the object into a straight NSDictionary or NSArray first. Write a method called jsonRepresentation and then one initFromJSONRepresentation. Not very hard in most cases.

Upvotes: 3

Related Questions