Esempio n. 1
0
        private JsonSerializationForgePair GetForgePair(
            Type type,
            IDictionary <Type, JsonApplicationClassSerializationDesc> classSerializationDescs)
        {
            type = type.GetBoxedType();

            // Search for a pre-built pair.  Please change this logic so that built-in types are
            // done in pairs.

            if (_forgePairs.TryGetValue(type, out var forgePair))
            {
                return(forgePair);
            }

            // The type was not found as a pre-built pair.  Examine the data type and attempt to create
            // the right pair of forges for this type.

            if (type.IsEnum)
            {
                return(new JsonSerializationForgePair(
                           JsonSerializerForgeStringWithToString.INSTANCE,
                           new JsonDeserializerForgeEnum(type)));
            }

            if (type.IsArray)
            {
                var componentType      = type.GetElementType();
                var componentForgePair = GetForgePair(componentType, classSerializationDescs);
                return(new JsonSerializationForgePair(
                           new JsonSerializerForgeArray(componentForgePair.SerializerForge, componentType),
                           new JsonDeserializerForgeArray(componentForgePair.DeserializerForge, componentType)));
            }

            if (type.IsGenericStringDictionary())
            {
                var valueType          = type.GetDictionaryValueType();
                var valueTypeForgePair = GetForgePair(valueType, classSerializationDescs);

                return(new JsonSerializationForgePair(
                           new JsonSerializerForgePropertyMap(
                               valueTypeForgePair.SerializerForge,
                               valueType),
                           new JsonDeserializerForgePropertyMap(
                               valueTypeForgePair.DeserializerForge,
                               valueType)));
            }

            if (type.IsGenericList() || type.IsGenericEnumerable())
            {
                var genericType = GenericExtensions.GetComponentType(type);
                if (genericType == null)
                {
                    return(null);
                }

                var genericForgePair = GetForgePair(genericType, classSerializationDescs);
                return(new JsonSerializationForgePair(
                           new JsonSerializerForgeArray(genericForgePair.SerializerForge, genericType),
                           new JsonDeserializerForgeArray(genericForgePair.DeserializerForge, genericType)));
            }

            if (classSerializationDescs.TryGetValue(type, out JsonApplicationClassSerializationDesc existingDesc))
            {
                return(new JsonSerializationForgePair(
                           new JsonSerializerForgeByClassName(existingDesc.SerializerClassName),
                           new JsonDeserializerForgeByClassName(existingDesc.DeserializerClassName)));
            }

            //throw new ArgumentException($"unable to determine forge pair for type {type.CleanName()}");

            return(null);
        }
Esempio n. 2
0
        public SchemaXMLEventType(
            EventTypeMetadata eventTypeMetadata,
            ConfigurationCommonEventTypeXMLDOM config,
            SchemaModel schemaModel,
            string representsFragmentOfProperty,
            string representsOriginalTypeName,
            EventBeanTypedEventFactory eventBeanTypedEventFactory,
            EventTypeNameResolver eventTypeResolver,
            XMLFragmentEventTypeFactory xmlEventTypeFactory)
            : base(
                eventTypeMetadata,
                config,
                eventBeanTypedEventFactory,
                eventTypeResolver,
                xmlEventTypeFactory)
        {
            propertyGetterCache = new Dictionary<string, EventPropertyGetterSPI>();
            SchemaModel = schemaModel;
            rootElementNamespace = config.RootElementNamespace;
            schemaModelRoot = SchemaUtil.FindRootElement(schemaModel, rootElementNamespace, RootElementName);
            isPropertyExpressionXPath = config.IsXPathPropertyExpr;
            RepresentsFragmentOfProperty = representsFragmentOfProperty;
            RepresentsOriginalTypeName = representsOriginalTypeName;

            // Set of namespace context for XPath expressions
            var ctx = new XPathNamespaceContext();
            if (config.DefaultNamespace != null) {
                ctx.SetDefaultNamespace(config.DefaultNamespace);
            }

            foreach (var entry in config.NamespacePrefixes) {
                ctx.AddNamespace(entry.Key, entry.Value);
            }

            NamespaceContext = ctx;

            // add properties for the root element
            IList<ExplicitPropertyDescriptor> additionalSchemaProps = new List<ExplicitPropertyDescriptor>();

            // Add a property for each complex child element
            foreach (SchemaElementComplex complex in schemaModelRoot.ComplexElements) {
                var propertyName = complex.Name;
                var returnType = typeof(XmlNode);
                Type propertyComponentType = null;

                if (complex.OptionalSimpleType != null) {
                    returnType = SchemaUtil.ToReturnType(complex);
                    if (returnType == typeof(string)) {
                        propertyComponentType = typeof(char);
                    }
                }

                if (complex.IsArray) {
                    returnType = typeof(XmlNode[]); // We use Node[] for arrays and NodeList for XPath-Expressions returning Nodeset
                    propertyComponentType = typeof(XmlNode);
                }

                var isFragment = false;
                if (ConfigurationEventTypeXMLDOM.IsAutoFragment && !ConfigurationEventTypeXMLDOM.IsXPathPropertyExpr) {
                    isFragment = CanFragment(complex);
                }

                var getter = DoResolvePropertyGetter(propertyName, true);
                var desc = new EventPropertyDescriptor(
                    propertyName,
                    returnType,
                    propertyComponentType,
                    false,
                    false,
                    complex.IsArray,
                    false,
                    isFragment);
                var @explicit = new ExplicitPropertyDescriptor(desc, getter, false, null);
                additionalSchemaProps.Add(@explicit);
            }

            // Add a property for each simple child element
            foreach (var simple in schemaModelRoot.SimpleElements) {
                var propertyName = simple.Name;
                var returnType = SchemaUtil.ToReturnType(simple);
                var componentType = GenericExtensions.GetComponentType(returnType);
                var isIndexed = simple.IsArray || componentType != null;
                var getter = DoResolvePropertyGetter(propertyName, true);
                var desc = new EventPropertyDescriptor(
                    propertyName,
                    returnType,
                    componentType,
                    false,
                    false,
                    isIndexed,
                    false,
                    false);
                var @explicit = new ExplicitPropertyDescriptor(desc, getter, false, null);
                additionalSchemaProps.Add(@explicit);
            }

            // Add a property for each attribute
            foreach (var attribute in schemaModelRoot.Attributes) {
                var propertyName = attribute.Name;
                var returnType = SchemaUtil.ToReturnType(attribute);
                var componentType = GenericExtensions.GetComponentType(returnType);
                var isIndexed = componentType != null;
                var getter = DoResolvePropertyGetter(propertyName, true);
                var desc = new EventPropertyDescriptor(
                    propertyName,
                    returnType,
                    componentType,
                    false,
                    false,
                    isIndexed,
                    false,
                    false);
                var @explicit = new ExplicitPropertyDescriptor(desc, getter, false, null);
                additionalSchemaProps.Add(@explicit);
            }

            // Finally add XPath properties as that may depend on the rootElementNamespace
            Initialize(config.XPathProperties.Values, additionalSchemaProps);
        }