0

I'm working with an API that returns JSON. All of the serializing and deserializing has been straightforward so far using the JavaScriptSerializer class from System.Web.Script.Serialization. There is an an endpoint that can return errors and doesn't use KVP in its errors list (not sure why, violates their structure for the rest of the api).

I'm having trouble figuring out the class structure to deserialize it.

Example return:

{
"status": "failed",
"errors": [
    [
        ["base", "error details 1"], 
        ["base", "error details 2"], 
        ["base", "error details 3"]
    ]
]
}

This is a confusing data structure since everything else is paired. But anyway, I've tried using arrays and lists for the errors piece. Here are my classes:

<Serializable> _
Public Class SearchResult
    Public Property status As String
    Public Property id As Integer
    Public Property errors As List(Of APIError)

    Public Sub New()
        errors = New List(Of APIError)
    End Sub

    Public Shared Function Deserialize(ByVal json_string As String) As SearchResult
        Dim result As SearchResult

        Dim jss As New JavaScriptSerializer()

        Try
            result = jss.Deserialize(json_string, GetType(SearchResult))
        Catch ex As Exception
            result = Nothing
            Debug.WriteLine("Failed to deserialize " & json_string)
            Debug.WriteLine(ex.Message)
        End Try

        Return result
    End Function
End Class

And the API Error Class

<Serializable> _
Public Class APIError
    Public Property error_fields() As String
End Class

I have tried making this a list and an array. I am continually getting an exception message that the Class is not supported for deserialization of an array.

I would prefer to use the JSS for deserializing and serializing as I have a difficult time selling my boss on third-party libraries.

Where am I going wrong? Thanks!

cdownard
  • 219
  • 2
  • 14

1 Answers1

0

That is one deeply nested array. Try something like this (untested in VB format):

<Serializable> _
Public Class SearchResult

    Public Property status As String    
    Public Property errors As List(Of List(Of string()))

    Public Sub New()
        errors = New List(Of List(Of string()))
        errors.Add(new List(Of string()))
    End Sub

    Public Shared Function Deserialize(ByVal json_string As String) As SearchResult
        Dim result As SearchResult

        Dim jss As New JavaScriptSerializer()

        Try
            result = jss.Deserialize(json_string, GetType(SearchResult))
        Catch ex As Exception
            result = Nothing
            Debug.WriteLine("Failed to deserialize " & json_string)
            Debug.WriteLine(ex.Message)
        End Try

        Return result
    End Function
End Class

The original C# that I tested with is this (and it does work):

namespace JavaScriptSerializerApp
{
    class Program
    {
        static void Main(string[] args)
        {

            SearchResult sr = new SearchResult();
            sr.status = "failed";
            sr.errors[0].Add(new string[] { "base", "error details 1" });
            sr.errors[0].Add(new string[] { "base", "error details 2" });
            sr.errors[0].Add(new string[] { "base", "error details 3" });

            JavaScriptSerializer jss = new JavaScriptSerializer();

            string output1 = jss.Serialize(sr);

            string json = "{\"status\": \"failed\",\"errors\": [[[\"base\", \"error details 1\"],[\"base\", \"error details 2\"],[\"base\", \"error details 3\"]]]}";
            SearchResult sr2 = (SearchResult)jss.Deserialize(json, typeof(SearchResult));
            string output2 = jss.Serialize(sr2);

            Console.WriteLine((output1 == output2).ToString());

        }
    }

    [Serializable()]
    public class SearchResult
    {
        public SearchResult()
        {
            errors = new List<List<string[]>>();
            errors.Add(new List<string[]>());

        }

        public string status { get; set; }
        public List<List<string[]>> errors { get; set; }
    }

}
tcarvin
  • 10,715
  • 3
  • 31
  • 52
  • Well, you got it. I was just trying `Public Property errors As List(Of List(Of List(Of String)))` (wow that's ugly). Yours results in a non infinite nest (or seemingly infinite anyway - debugger was just going continuous). Thanks! – cdownard Jun 03 '14 at 20:35