JsonConvert.SerializeObject not working

Feb 24, 2010 at 9:29 PM

Hi,

I just downloaded the jsconvert zip file and placed the newtonsoft.Json.dll file (and the pdb + xml) into the bin directory of my project.
I also added a reference to the dll.

I set up a very basic class product:

 

Public Class product
    Inherits System.Web.UI.Page

    Public name As String
    Public gewicht As Integer
End Class

 

and in my aspx file in the page load I placed:

 

        Dim p1 As New product
        p1.name = "apple"
        p1.gewicht = 1

 lblp.text = Newtonsoft.Json.JsonConvert.SerializeObject(p1)
Whenever I try to build / debug the project I get an error on the serializeobject part and it wants to load the file:
JsonSerializerInternalWriter.cs

The error it throws is:
Error getting value from 'Request' on project.product
and it halts at line 306 of the above .cs file which is the throw; at the very bottom (underline) of the following code:
private void SerializeObject(JsonWriter writer, object value, JsonObjectContract contract)
{
contract.InvokeOnSerializing(value, Serializer.Context);

string s;
if (TryConvertToString(value, contract.UnderlyingType, out s))
{
writer.WriteValue(s);
contract.InvokeOnSerialized(value, Serializer.Context);

return;
}

SerializeStack.Add(value);
writer.WriteStartObject();

bool isReference = contract.IsReference ?? HasFlag(Serializer.PreserveReferencesHandling, PreserveReferencesHandling.Objects);
if (isReference)
{
writer.WritePropertyName(JsonTypeReflector.IdPropertyName);
writer.WriteValue(Serializer.ReferenceResolver.GetReference(value));
}
if (HasFlag(Serializer.TypeNameHandling, TypeNameHandling.Objects))
{
WriteTypeProperty(writer, contract.UnderlyingType);
}

int initialDepth = writer.Top;

foreach (JsonProperty property in contract.Properties)
{
try { if (!property.Ignored && property.Readable)
{
object memberValue = property.ValueProvider.GetValue(value);
JsonContract memberContract = GetContractSafe(memberValue);

WriteMemberInfoProperty(writer, memberValue, property, memberContract);
}
}
catch (Exception ex)
{
if (IsErrorHandled(value, contract, property.PropertyName, ex))
HandleError(writer, initialDepth);
else throw;
}
}




I have no idea why I can't get it to work. It all seems so simple on the homepage  :(

 

Anyone have any idea what's causing this?

Thx in advance!

 

Coordinator
Feb 24, 2010 at 10:46 PM

Your product class is inheriting from System.Web.UI.Page.....

Feb 25, 2010 at 7:33 AM

Yikes .. what was I thinking of :-(

Got it to work now!

removed the inherit in the class, imports project.product in the aspx.vb file and inherits system.web.ui.page in the aspx.vb file and it's working

Thanks for the help James!