public override object ReadJson(JsonReader reader, Type objectType, object existingValue,
                                        JsonSerializer serializer)
        {
            var jobject = JObject.Load(reader);

            object resolvedType = null;

            var kind = jobject["kind"]?.Value <string>();

            if (kind != null)
            {
                switch (kind)
                {
                case "ec":
                    resolvedType = new EcElement();
                    break;

                case "em":
                    resolvedType = new EmElement();
                    break;

                case "ph":
                    resolvedType = new PhElement();
                    break;

                case "sc":
                    resolvedType = new ScElement();
                    break;

                case "sm":
                    resolvedType = new SmElement();
                    break;
                }
            }
            else
            {
                resolvedType = new TextElement();
            }


            serializer.Populate(jobject.CreateReader(), resolvedType);

            return(resolvedType);
        }
示例#2
0
        public void PhElement(XlfEventArgs args)
        {
            if (!args.NodeType.Equals("EndElement"))
            {
                object parent = stack.Peek();
                switch (parent)
                {
                case Segment s:
                    PhElement source1 = new PhElement();
                    source1.Attributes = args.Attributes;
                    if (args.sourceOrTarget.Equals("source"))
                    {
                        s.Source.Add(source1);
                    }
                    else
                    {
                        s.Target.Add(source1);
                    }
                    break;

                case Ignorable i:
                    PhElement source2 = new PhElement();
                    if (args.sourceOrTarget.Equals("target"))
                    {
                        i.Source.Add(source2);
                    }
                    else
                    {
                        i.Target.Add(source2);
                    }
                    break;

                default:
                    throw new Exception("Was expecting a Segment or Ignorable object.");
                    break;
                }
            }
        }