Understanding JsonConvert.ExportToString

185 views
Skip to first unread message

Jo

unread,
Feb 5, 2009, 11:21:27 PM2/5/09
to Jayrock
Hi

I am new to Jayrock and trying to play around with
JsonConvert.ExportToString and my C# classes. What I observed while
doing this is that JsonConvert.ExportToString uses Equals method.
After adding more exception handling I could export the following
class to Json.

public class CDictionaryResponse : ITranasactContent
{
public CRequester Requester;
public List<CGroupCategory> GroupCategories;
public List<CLocation> Origins;
public List<CLocation> Destinations;
public List<CError> Errors;

public CDictionaryResponse()
{
Requester = new CRequester();
Origins = new List<CLocation>();
Destinations = new List<CLocation>();
GroupedOrigins = new List<CLocationHierarchy>();
Errors = new List<CError>();
}
}

CLocation in turn looks like:

public class CLocation: ICloneable
{
public string Name;
public string Code;
public int ID;
public int CountryId;

public object Clone()
{
return MemberwiseClone();
}

public CLocation()
{
Name = string.Empty;
Code = string.Empty;
ID = 0;
CountryId = 0;
}

public CLocation(string p_strName,
string p_strCode, int p_nID)
{
Name = p_strName;
Code = p_strCode;
ID = p_nID;
}

public override bool Equals(object obj)
{
CLocation objLocation = obj as CLocation;

if (objLocation == null)
return false;

if (this.ID.Equals(objLocation.ID))
return true;

return false;
}

public override int GetHashCode()
{
return base.GetHashCode();
}
}


Then I modified my CDictionaryResponse class to add one more member
public List<CLocationHierarchy> GroupedOrigins;

CLocation looks like:
public class CLocationHierarchy
{
public CLocation ParentLocation;
List<CLocation> ChildLocations;

public CLocationHierarchy(CLocation p_objLocation)
{
ParentLocation = p_objLocation;
ChildLocations = new List<CLocation>();
}

public bool IsParentEqual(CLocation p_objLocation)
{
return ParentLocation.Equals(p_objLocation);
}

public void AddChild(CLocation p_objChild)
{
if(ChildLocations == null)
ChildLocations = new List<CLocation>();

if(!ChildLocations.Contains(p_objChild))
ChildLocations.Add(p_objChild);
}

//public override bool Equals(object obj)
//{
// if (obj == null)
// return false;

// if (!(obj is CLocationHierarchy))
// return false;

// CLocationHierarchy objToCompare = (CLocationHierarchy)
obj;
// if (objToCompare == null)
// return false;

// return ParentLocation.Equals
(objToCompare.ParentLocation);
//}

public override bool Equals(object obj)
{
return base.Equals(obj);
}
}

I have been receiving exceptions for the overridden Equals method in
this class. After invoking ExportToString, the equals method is called
with obj with type JsonNull (which fails the condition obj == null).

The other greater problem is that, the list of CLocationHierarchy is
not exported to string the way CLocation is. If I have 5 objects of
CLocationHierarchy in CDictionaryResponse, then the json string shows
'CLocationHierarchy' 5 times and not the actual values - whereas for
other members, it works fine.

Could I have access to how to start kind of page on JayRock ?

Thanks
Jyotsna













Atif Aziz

unread,
Feb 6, 2009, 3:10:01 AM2/6/09
to jay...@googlegroups.com
>>
I have been receiving exceptions for the overridden Equals method in
this class.
<<

What exception have you been receiving?

> which fails the condition obj == null.

How is that failing? You mean an exception occurs or the condition returns false (which I think is normal). I can't see an issue right away from reading your Equals implementations.

>>
The other greater problem is that, the list of CLocationHierarchy is
not exported to string the way CLocation is. If I have 5 objects of
CLocationHierarchy in CDictionaryResponse, then the json string shows
'CLocationHierarchy' 5 times and not the actual values
<<

If Jayrock does not know how to export a type, like your CLocationHierarchy, it resorts to simply calling ToString on the instance of the type and that's what you're seeing in the output. You can tell Jayrock how to export a type by registering an exporter. When you don't do that, however, it tries to export a type using a generic scheme subject to a few conditions:

1. The type is public
2. The type is not a primitive type
3. The type has an default constructor (one that takes no arguments)
4. The type has one or more read/write properties
5. The type is anonymous (C# 3)

You CLocationHierarchy is failing the 3rd condition. If you add a default constructor, you should be fine.

> Could I have access to how to start kind of page on JayRock ?

Not sure I understand what you mean here. Could you clarify further?
Reply all
Reply to author
Forward
0 new messages