0

I'm trying to do a GET request and return a List of clsGWKlant objects. But I keep getting an JsonSerializationException for the property prpCreditCardNummer saying that the conversion from string to double is invalid. When I use the debugger I can see that the clsGWKlant objects are correctly added to the list without any errors. But returning the list gives the error. I am also wondering where the cast to Double error in the exception message comes from? The clsGWKlant and clsGWaySnelStart are types from a dll. How to fix this error?

I tried:

GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
GlobalConfiguration.Configuration.Formatters.Remove(GlobalConfiguration.Configuration.Formatters.XmlFormatter);  

Which doesn't work, also I want my Web API to still be able to return the response in JSON and XML

Error when doing a GET with the Advanced Rest Client Application (Google Chrome extension)

{
Message: "An error has occurred."
ExceptionMessage: "The 'ObjectContent`1' type failed to serialize the response body for content type 'application/json; charset=utf-8'."
ExceptionType: "System.InvalidOperationException"
StackTrace: null
InnerException: {
Message: "An error has occurred."
ExceptionMessage: "Error getting value from 'prpCreditCardNummer' on 'SnelStartGatewayNET.clsGWKlant'."
ExceptionType: "Newtonsoft.Json.JsonSerializationException"
StackTrace: " at Newtonsoft.Json.Serialization.DynamicValueProvider.GetValue(Object target) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CalculatePropertyValues(JsonWriter writer, Object value, JsonContainerContract contract, JsonProperty member, JsonProperty property, JsonContract& memberContract, Object& memberValue) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter writer, IEnumerable values, JsonArrayContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value, Type objectType) at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value, Type objectType) at System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, Encoding effectiveEncoding) at System.Net.Http.Formatting.JsonMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, Encoding effectiveEncoding) at System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, HttpContent content) at System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStreamAsync(Type type, Object value, Stream writeStream, HttpContent content, TransportContext transportContext, CancellationToken cancellationToken) --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.WebHost.HttpControllerHandler.<WriteBufferedResponseContentAsync>d__1b.MoveNext()"
InnerException: {
Message: "An error has occurred."
ExceptionMessage: "The conversion from string to type Double is invalid."
ExceptionType: "System.InvalidCastException"
StackTrace: " at SnelStartGatewayNET.clsGWKlant.get_prpCreditCardNummer() at GetprpCreditCardNummer(Object ) at Newtonsoft.Json.Serialization.DynamicValueProvider.GetValue(Object target)"
InnerException: {
Message: "An error has occurred."
ExceptionMessage: "Input string was not in a correct format."
ExceptionType: "System.FormatException"
StackTrace: " at Microsoft.VisualBasic.CompilerServices.Conversions.ParseDouble(String Value, NumberFormatInfo NumberFormat) at Microsoft.VisualBasic.CompilerServices.Conversions.ToDouble(String Value, NumberFormatInfo NumberFormat)"
}-
}-
}-
}

HTTP GET method

[HttpGet]
public IEnumerable<clsGWKlant> Get()
{
    var klant = new clsGWKlant();
    var klanten = new List<clsGWKlant>();

    var gway = new clsGWaySnelStart();

    gway.mtdGWayAdmiOpenenViaLoginSettings(0, Extensions.LoginSettings("databasename"));

    var done = false;
    while (!done)
    {
        if (klant.mtdGWayKlantReadNext() == false)
        {
            Console.WriteLine("No more customers found");
            done = true;
            break;
        }
        klanten.Add(klant);
    }

    gway.mtdGWayAdmiSluiten();

    return klanten;
}

clsGWKlant.cs creditcardnumber property

public int prpCreditCardNummer
{
  get
  {
    try
    {
      return Conversions.ToInteger(this.varClsGWKlantType.GetProperty("prpCreditCardNummer").GetValue(RuntimeHelpers.GetObjectValue(this.varClsGWKlant), new object[0]));
    }
    catch (Exception ex)
    {
      ProjectData.SetProjectError(ex);
      throw ex.InnerException;
    }
  }
  set
  {
    try
    {
      this.varClsGWKlantType.GetProperty("prpCreditCardNummer").SetValue(RuntimeHelpers.GetObjectValue(this.varClsGWKlant), (object) value, new object[0]);
    }
    catch (Exception ex)
    {
      ProjectData.SetProjectError(ex);
      throw ex.InnerException;
    }
  }
}
Sybren
  • 1,071
  • 3
  • 17
  • 51
  • 2
    for one thing, I don't think your int is going to be able to hold a credit card number, since they tend to be 16 digits long, and a 32-bit integer can only hold a figure of up to ~2.1 billion, which is 10 digits. – user1666620 Dec 02 '15 at 10:55
  • 1
    additionally, saving a credit card number as any number type (or other data type that would omit zeroes at the start) is a bad idea : see http://stackoverflow.com/questions/7164858/can-credit-card-numbers-contain-leading-zeros – Timothy Groote Dec 02 '15 at 10:57
  • Yes you're right about that but in the database where the creditcardnumbers are stored they all have the value : `12345678` so that should give any problems right, I didn't design/create the dll so for now I'm just trying to make it this work. – Sybren Dec 02 '15 at 11:01
  • should = shouldn't ^ – Sybren Dec 02 '15 at 11:25

0 Answers0