1

I have an object that has custom data types in it. When I try to pass that object through my REST API, I get an error that says: The 'ObjectContent'1' type failed to serialize the response body for content type 'application/json; charset=utf-8'. I am assuming that the API is attempting to serialize my custom object (with custom data types) in order to convert it to JSON. Is there a way I can make this possible? I am coding in visual basic.

EDIT: I am using ASP.NET Framework 4.6.1. My object is involved with connecting to a database. Because of that, I have created custom datatypes such as databasePointer which is simply a long value. This is so I don't mix up numbers when connecting to the database. I simply need to find out how to convert those custom data types back to primitive data types before I pass my object to be serialized.

Jacob
  • 299
  • 1
  • 18
  • You may consider sharing more info. What framework are you implementing your REST api in and what does your object look like? – Deadron Jul 28 '17 at 17:12
  • Thanks for the input @Deadron – Jacob Jul 28 '17 at 17:22
  • We need to see a [mcve] to give you any specific help. Answers to similar-sounding questions include [failed to serialize the response in Web API](https://stackoverflow.com/q/12641386), [Failed to serialize the response in Web API with Json](https://stackoverflow.com/q/23098191), [Asp.Net Web API Error: The 'ObjectContent`1' type failed to serialize the response body for content type 'application/xml; charset=utf-8'](https://stackoverflow.com/q/13959048) and maybe [Web API: Failed to serialize the response body for content type](https://stackoverflow.com/q/33559080). – dbc Jul 30 '17 at 06:43
  • Simply adding the full `ToString()` output of the exception including the exception type, complete message, traceback **and inner exception** would help us to help you - but a [mcve] would be best. For more on how to ask a question that's likely to get an answer, see [ask] and https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/ – dbc Jul 30 '17 at 06:44

1 Answers1

0

I ended up simply taking the object, running an iteration of properties (that I chose so they were primitive values), and copying them over to another object. I then passed that through and it worked fine. I had to write a class to make the conversion for the specific object, but it works well and didn't take long.

Example class:

Public Class Converter
Dim propertyKeys As New ArrayList
Private Sub InitiateKeys()
'add keys here for property names
End Sub
Public Function convertToString(ByVal order As ShopOrder) As 

   InitiateKeys()

    Dim json As ShopOrderJSON = New ShopOrderJSON

    If (IsDBNull(order)) Then
        Return json
    End If

    Try
        For Each key As String In shopOrderKeys
            json.GetType.GetField(key).SetValue(json, order.GetType.GetField(key).GetValue(order).ToString)
        Next
    Catch ex As Exception
        Console.WriteLine(ex.Message)
    End Try

    Return json
End Function
End Class
Jacob
  • 299
  • 1
  • 18