コード例 #1
0
        internal static bool ShouldBeDictionary(
            this JsonSchema schema,
            string typeName,
            string propertyName,
            HintDictionary hintDictionary,
            out DictionaryHint dictionaryHint)
        {
            dictionaryHint = null;

            // Ignore any DictionaryHint that might apply to this property
            // if the property is not an object.
            if (schema.SafeGetType() != SchemaType.Object)
            {
                return(false);
            }

            // Is there a DictionaryHint that targets this property?
            if (hintDictionary == null)
            {
                return(false);
            }

            dictionaryHint = hintDictionary.GetPropertyHint <DictionaryHint>(typeName, propertyName);
            if (dictionaryHint == null)
            {
                return(false);
            }

            return(true);
        }
コード例 #2
0
        private TypeSyntax MakeDictionaryType(
            IList <KeyValuePair <string, PropertyInfo> > entries,
            string propertyName,
            DictionaryHint dictionaryHint,
            JsonSchema dictionaryElementSchema)
        {
            string key = MakeDictionaryItemKeyName(propertyName);

            string keyTypeName = dictionaryHint?.KeyTypeName ?? WellKnownTypeNames.String;

            // An explicitly hinted value type takes precedence over what's declared in
            // the schema.
            TypeSyntax valueType;

            if (dictionaryHint?.ValueTypeName != null)
            {
                valueType = SyntaxFactory.ParseTypeName(dictionaryHint.ValueTypeName);
                entries.Add(new KeyValuePair <string, PropertyInfo>(
                                key,
                                new PropertyInfo(
                                    description: string.Empty,
                                    serializedName: null,
                                    comparisonKind: dictionaryHint.ComparisonKind,
                                    hashKind: dictionaryHint.HashKind,
                                    initializationKind: dictionaryHint.InitializationKind,
                                    type: valueType,
                                    namespaceName: dictionaryHint.NamespaceName,
                                    isRequired: true,
                                    defaultValue: null,
                                    isOfSchemaDefinedType: false,
                                    arrayRank: 0,
                                    declarationOrder: 0)));
            }
            else
            {
                AddPropertyInfoFromPropertySchema(entries, key, dictionaryElementSchema, isRequired: true);
                PropertyInfo info = entries.Single(kvp => kvp.Key == key).Value;
                valueType = info.Type;
            }

            // Create a dictionary of whatever this property is. If the property
            // is an array, this will result in a dictionary of lists, and so on.
            return(SyntaxFactory.GenericName(
                       SyntaxFactory.Identifier("IDictionary"),
                       SyntaxFactory.TypeArgumentList(
                           SyntaxFactory.SeparatedList(
                               new TypeSyntax[]
            {
                SyntaxFactory.ParseTypeName(keyTypeName),
                valueType
            }))));
        }