Exemplo n.º 1
0
            public override void Write(Utf8JsonWriter writer, BusinessObject value, JsonSerializerOptions options)
            {
                var boTypeString = value.GetBoTyp();

#pragma warning disable CS0618 // Type or member is obsolete
                var boType = BoMapper.GetTypeForBoName(boTypeString);
#pragma warning restore CS0618 // Type or member is obsolete
                System.Text.Json.JsonSerializer.Serialize(writer, value, boType, options);
            }
Exemplo n.º 2
0
            public override BusinessObject Read(ref Utf8JsonReader reader, Type typeToConvert,
                                                JsonSerializerOptions options)
            {
                if (reader.TokenType == JsonTokenType.Null)
                {
                    return(null);
                }
                if (typeToConvert.IsAbstract)
                {
                    var jdoc = JsonDocument.ParseValue(ref reader);
                    if (!jdoc.RootElement.TryGetProperty("BoTyp", out var boTypeProp))
                    {
                        boTypeProp = jdoc.RootElement.GetProperty("boTyp");
                    }
                    var boTypeString = boTypeProp.GetString();
#pragma warning disable CS0618 // Type or member is obsolete
                    var boType = BoMapper.GetTypeForBoName(boTypeString);
#pragma warning restore CS0618 // Type or member is obsolete
                    if (boType == null)
                    {
                        foreach (var assembley in AppDomain.CurrentDomain.GetAssemblies())
                        {
                            try
                            {
                                boType = assembley.GetTypes().FirstOrDefault(x =>
                                                                             string.Equals(x.Name, boTypeString, StringComparison.CurrentCultureIgnoreCase));
                            }
                            catch (ReflectionTypeLoadException)
                            {
                                continue;
                            }

                            if (boType != null)
                            {
                                break;
                            }
                        }

                        if (boType == null)
                        {
                            throw new NotImplementedException(
                                      $"The type '{boTypeString}' does not exist in the BO4E standard.");
                        }
                    }

                    return(System.Text.Json.JsonSerializer.Deserialize(jdoc.RootElement.GetRawText(), boType, options)
                           as BusinessObject);
                }

                return(null);
            }
Exemplo n.º 3
0
        public void TestBoNameTyping()
        {
            Assert.AreEqual(typeof(Benachrichtigung), BoMapper.GetTypeForBoName("Benachrichtigung"));
            Assert.AreEqual(typeof(Benachrichtigung), BoMapper.GetTypeForBoName("bEnAcHriCHTIGuNg"));

            Assert.ThrowsException <ArgumentNullException>(() => BoMapper.GetTypeForBoName(null), "null as argument must result in a ArgumentNullException");

            /*
             * bool argumentExceptionThrown = false;
             * try
             * {
             *  BoMapper.GetTypeForBoName("dei mudder ihr business object");
             * }
             * catch (ArgumentException)
             * {
             *  argumentExceptionThrown = true;
             * }
             * Assert.IsTrue(argumentExceptionThrown, "invalid argument must result in a ArgumentException");
             */
            Assert.IsNull(BoMapper.GetTypeForBoName("dei mudder ihr business object"), "invalid business object names must result in null");
        }
Exemplo n.º 4
0
            public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
            {
                if (reader.TokenType == JsonToken.Null)
                {
                    return(null);
                }
                if (objectType.IsAbstract)
                {
                    JObject jo = JObject.Load(reader);
                    Type    boType;
                    if (serializer.TypeNameHandling.HasFlag(TypeNameHandling.Objects) && jo.TryGetValue("$type", out JToken typeToken))
                    {
                        boType = BusinessObjectSerializationBinder.BusinessObjectAndCOMTypes.SingleOrDefault(t => typeToken.Value <string>().ToUpper().StartsWith(t.FullName.ToUpper()));
                    }
                    else if (!jo.ContainsKey("boTyp"))
                    {
                        throw new ArgumentException("If deserializing into an abstract BusinessObject the key \"boTyp\" has to be set. But it wasn't.");
                    }
                    else
                    {
#pragma warning disable CS0618                                                            // Type or member is obsolete
                        boType = BoMapper.GetTypeForBoName(jo["boTyp"].Value <string>()); // ToDo: catch exception if boTyp is not set and throw exception with descriptive error message
#pragma warning restore CS0618                                                            // Type or member is obsolete
                    }
                    if (boType == null)
                    {
                        foreach (var assembley in AppDomain.CurrentDomain.GetAssemblies())
                        {
                            try
                            {
                                boType = assembley.GetTypes().FirstOrDefault(x => x.Name.ToUpper() == jo["boTyp"].Value <string>().ToUpper());
                            }
                            catch (ReflectionTypeLoadException)
                            {
                                continue;
                            }
                            if (boType != null)
                            {
                                break;
                            }
                        }
                        if (boType == null)
                        {
                            throw new NotImplementedException($"The type '{jo["boTyp"].Value<string>()}' does not exist in the BO4E standard.");
                        }
                    }
                    var deserializationMethod = serializer.GetType() // https://stackoverflow.com/a/5218492/10009545
                                                .GetMethods()
                                                .Where(m => m.Name == nameof(serializer.Deserialize))
                                                .Select(m => new
                    {
                        Method = m,
                        Params = m.GetParameters(),
                        Args   = m.GetGenericArguments()
                    })
                                                .Where(x => x.Params.Length == 1 &&
                                                       x.Args.Length == 1)
                                                .Select(x => x.Method)
                                                .First()
                                                .GetGenericMethodDefinition()
                                                .MakeGenericMethod(new Type[] { boType });
                    try
                    {
                        return(deserializationMethod.Invoke(serializer, new object[] { jo.CreateReader() }));
                    }
                    catch (TargetInvocationException tie) when(tie.InnerException != null)
                    {
                        throw tie.InnerException; // to hide the reflection to the outside.
                    }
                }
                else
                {
                    serializer.ContractResolver.ResolveContract(objectType).Converter = null;
                    return(serializer.Deserialize(JObject.Load(reader).CreateReader(), objectType));
                }
            }