Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8729581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:51:30+00:00 2026-06-13T08:51:30+00:00

I have a recursive data structure. Something like… Public Class Comparison Property Id As

  • 0

I have a recursive data structure. Something like…

Public Class Comparison
    Property Id As Integer
End Class

Public Class SimpleComparison
    Inherits Comparison
    Property Left As String
    Property Right As String
End Class

Public Class ComplexComparison
    Inherits Comparison
    Property Left As Comparison
    Property Right As Comparison
End Class

I need to deserialize to this from JSON.

As you can see, the only way to determine whether to use a ComplexComparison or a SimpleComparison is by determining if the .Left value is a string or an object. (NB They’ll either both be string or both object)

So, I’m writing a custom converter and have got this far…

Public Class ComparisonConverter
    Inherits Newtonsoft.Json.JsonConverter
    ''<Snip>
Public Overrides Function ReadJson(reader As Newtonsoft.Json.JsonReader, objectType As Type, existingValue As Object, serializer As Newtonsoft.Json.JsonSerializer) As Object
    Dim obj As JObject = TryCast(serializer.Deserialize(Of JToken)(reader), JObject)
    If obj IsNot Nothing Then
            ''We''ve got something to work with
        Dim Id As Integer = obj("Id").ToObject(Of Integer)()

            ''Check if we''re instantiating a simple or a complex comparison
        If obj("Left").GetType.IsAssignableFrom(GetType(JValue)) Then
            ''LHS is a string - Simple...
            Return New SimpleComparison With {
                .Id = Id,
                .Left = obj("Left").ToObject(Of String)(),
                .Right = obj("Right").ToObject(Of String)()}
        Else
            Return New ComplexComparison With {
            .Id = Id,
            .Left = ???, '' <<Problem
            .Right = ???}'' <<Problem
        End If
    Else
        Return Nothing
    End If
End Function
End Class

The branch of the If that results from the object being complex is where I get stuck. How can I re-invoke the deserializer on the obj("Left") and obj("Right") (which are of type JToken) ? Or should I Cast them to JObject and then factor this code out into a seperate function and recursively call that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T08:51:31+00:00Added an answer on June 13, 2026 at 8:51 am

    It turned out to be simpler than I expected and JSON.Net does all the heavy lifting for me…

    Public Overrides Function ReadJson(reader As Newtonsoft.Json.JsonReader, objectType As Type, existingValue As Object, serializer As Newtonsoft.Json.JsonSerializer) As Object
        Dim Ret As Comparison
        Dim JComparison As JObject = JObject.Load(reader)
        If JComparison("Left").GetType.IsAssignableFrom(GetType(JValue)) Then
            Ret = New SimpleComparison
        Else
            Ret = New ComplexComparison
        End If
        serializer.Populate(JComparison.CreateReader(), Ret)
        Return Ret
    End Function
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data structure that looks like this: public class Node { public
I have a recursive model like this: public class Node { public int Id
Let's say I have a recursive data structure class Tree { private Tree right;
I have a data-structure (in plist) that looks something like this: What i have
I have a recursive data type like this: template<typename T> struct SomeType { std::map<T,
Imagine I have a recursive algebraic data type like this (Haskell syntax): data Expr
I have a recursive data structure that I want ASP.NET to walk in the
I have a nested, mutual recursive data structure , and want to associate computational
I have a recursive immutable data structure in ocaml which can be simplified to
I have a recursive function that uses ref to walk down a data structure

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.