简体   繁体   中英

How to model key-value pairs with nested collections in C#?

I'm trying to build a data structure in C# to hold dynamic data created in JavaScript and represented with JSON. I'm looking for advice on how to design the C# data structure to hold the JSON data in a way that makes it easy to search. I'm currently using .NET 4.0; 4.5 is a ways off for this particular project.

The data have the following properties:

  • a collection of key-value pairs
  • keys are unique
  • the values can be any of true , false , some string values, or a nested collection of key-value pairs
  • a few dozen top-level properties, and properties with nested collections have a half-dozen pairs in the collections

Here's an example of the JavaScript data:

{ 
    foo:  true,
    bar:  false,
    baz:  {
        a: true,
        b: false,
        c: "maybe"
    }
    quux: "maybe"
}

I want to work with the data in the following ways:

  • 1-time initialization: Once I create the data structure, I only need to read it, so an immutable structure is fine.
  • Search for existence of keys: I need to be able to determin if a given key is present in the data. Some keys are well-known, and others are dynamic, so I won't know all their names until runtime, so I'll have to look up the keys using strings. In the excample above, assume I know ahead of time about foo , bar and baz , but quux is new to me and I don't know ahead of time that it exists, if it exists, and if it will have a flat or nested value.
  • Get the 'flat' value for a key: values are true , false and a number of 'maybe' string values for 'flat' members. If necessary, I can convert this down to real boolean values (treating the 'maybe' values as true ), but I'd prefer to preserve the 'maybe' values. I think I can know all the 'maybe' values up front, so I could also build an Enum holding those values, and also throw true and false in there so I have a single type for the value.
  • Get the keys and values for nested data: Some of the keys have a value that is itself a collection of key-value pairs. I need to be able to distinguish these members, and search for the nested keys and their values. From what I've seen of the input data, I don't think the nesting goes any deeper than 1 level.

Since the keys are unique, I'm attracted to a Dictionary for speed of lookup and ease of use. But I'm a bit stumped on representing nested collections alongside flat values. This is ridiculously easy in a dynamic language like JavaScript; what's the corresponding idiom in C#?

The real corresponding structure in C# would be an anonymous object/class. I personally would prefer using a Dictionary, either using it with object as value type (then, check if the value for a key is another Dictionary, so you know if you have a nested collection or not) or use a wrapper-class, which holds a Dictionary or a flat value and has some fields like IsNestedCollection and whatever you need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM