Beispiel #1
0
        public static TabularJsonString Create(IReadOnlyDictionary <string, Type> fields, IEnumerable data)
        {
            var schema = new TabularDataSchema();

            foreach (var entry in fields)
            {
                schema.Fields.Add(new TabularDataSchemaField(entry.Key, entry.Value.ToTableFieldType()));
            }
            var tabularDataSet = new TabularDataSet(schema, data);

            return(tabularDataSet.ToJson());
        }
        private static (TabularDataSchema schema, JArray data) Generate(IEnumerable source)
        {
            var schema  = new TabularDataSchema();
            var fields  = new HashSet <string>();
            var members = new HashSet <(string name, Type type)>();
            var data    = new JArray();

            foreach (var item in source)
            {
                switch (item)
                {
                case IEnumerable <(string name, object value)> valueTuples:

                    var tuples = valueTuples.ToArray();

                    EnsureFieldsAreInitializedFromValueTuples(tuples);

                    var o = new JObject();
                    foreach (var tuple in tuples)
                    {
                        o.Add(tuple.name, JToken.FromObject(tuple.value ?? "NULL"));
                    }

                    data.Add(o);
                    break;

                case IEnumerable <KeyValuePair <string, object> > keyValuePairs:

                    var pairs = keyValuePairs.ToArray();

                    EnsureFieldsAreInitializedFromKeyValuePairs(pairs);

                    var obj = new JObject();
                    foreach (var pair in pairs)
                    {
                        obj.Add(pair.Key, JToken.FromObject(pair.Value));
                    }

                    data.Add(obj);
                    break;

                default:
                    foreach (var memberInfo in item
                             .GetType()
                             .GetMembers(BindingFlags.Public | BindingFlags.Instance))
                    {
                        switch (memberInfo)
                        {
                        case PropertyInfo pi:
                            members.Add((memberInfo.Name, pi.PropertyType));
                            break;

                        case FieldInfo fi:
                            members.Add((memberInfo.Name, fi.FieldType));
                            break;
                        }
                    }

                    EnsureFieldsAreInitializedFromMembers();
                    data.Add(JObject.FromObject(item));
                    break;
                }
            }

            return(schema, data);

            void EnsureFieldsAreInitializedFromMembers()
            {
                foreach (var memberInfo in members)
                {
                    if (fields.Add(memberInfo.name))
                    {
                        schema.Fields.Add(new TabularDataSchemaField(memberInfo.name, memberInfo.type.ToTableFieldType()));
                    }
                }
            }

            void EnsureFieldsAreInitializedFromValueTuples(IEnumerable <(string name, object value)> valueTuples)
            {
                foreach (var(name, value) in valueTuples)
                {
                    if (fields.Add(name))
                    {
                        schema.Fields.Add(new TabularDataSchemaField(name, value?.GetType().ToTableFieldType()));
                    }
                }
            }

            void EnsureFieldsAreInitializedFromKeyValuePairs(IEnumerable <KeyValuePair <string, object> > keyValuePairs)
            {
                foreach (var keyValuePair in keyValuePairs)
                {
                    if (fields.Add(keyValuePair.Key))
                    {
                        schema.Fields.Add(new TabularDataSchemaField(keyValuePair.Key, keyValuePair.Value?.GetType().ToTableFieldType()));
                    }
                }
            }
        }
 public TabularDataSet(TabularDataSchema schema, IEnumerable data)
 {
     Schema = schema;
     Data   = data;
 }