예제 #1
0
        // protocol写入具体数据 基本类型直接写入 非基本类型需要递归进行写入
        // ex:oprot.writeString()
        // TODO:存在多次JSON decode enocde 性能问题!
        // TODO:value 为null如何处理

        private void writeVal(TProtocol oprot, GenericTree genericTree, object value, short id)
        {
            String thriftType = genericTree.getThrfitType();

            if (value == null)
            {
                return;
            }
            if (isPrimitiveType(genericTree))
            {
                oprot.IncrementRecursionDepth();
                try
                {
                    TField tField = getTField(genericTree.getName(), thriftType, id);
                    oprot.WriteFieldBegin(tField);
                    doProtocolMethod(oprot, thriftType, value, WRITE);
                    oprot.WriteFieldEnd();
                }
                finally
                {
                    oprot.DecrementRecursionDepth();
                }
            }
            else if (isColletionType(genericTree))
            {
                TField tField = getTField(genericTree.getName(), thriftType, id);
                oprot.WriteFieldBegin(tField);
                //TODO:假设目前集合只有List类型
                // String json = Newtonsoft.Json.JsonConvert.SerializeObject(value);
                if (isColletionMap(genericTree))
                {
                    GenericTree k = genericTree.getChildren()[PARAMINFO_COLLECTION_MAP_KEY];
                    GenericTree v = genericTree.getChildren()[PARAMINFO_COLLECTION_MAP_VALUE];
                    if (k == null || v == null)
                    {
                        Console.WriteLine("key or value is not found in GenericNode !");
                    }
                    else
                    {
                        IDictionary map = value as IDictionary;

                        oprot.WriteMapBegin(new TMap(TypeChange.TTypeMap[k.getThrfitType().ToUpper()]
                                                     , TypeChange.TTypeMap[v.getThrfitType().ToUpper()], map.Count));

                        foreach (DictionaryEntry entry in map)
                        {
                            //TODO:只支持key是简单类型
                            doProtocolMethod(oprot, k.getThrfitType(), entry.Key, WRITE);
                            if (v.getParamType() == TypeEnum.PRIMITIVE_TYPE)
                            {
                                doProtocolMethod(oprot, v.getThrfitType(), entry.Value, WRITE);
                            }
                            else
                            {
                                writeVal(oprot, v, entry.Value, id);
                            }
                        }
                        oprot.WriteMapEnd();
                    }
                }
                else
                {
                    // List<object> list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<object>>(json);
                    IEnumerable list = value as IEnumerable;

                    int count = 0;
                    foreach (var item in list)
                    {
                        count++;
                    }

                    GenericTree child           = genericTree.getChildren()[PARAMINFO_COLLECTION_INNER_KEY];
                    String      childThriftType = child.getThrfitType();
                    oprot.WriteListBegin(new TList(TypeChange.TTypeMap[childThriftType.ToUpper()], count));
                    //获取容器的孩子节点,如果集合是基础类型的 本次遍历就到这里
                    if (isPrimitiveType(child))
                    {
                        foreach (object obj in list)
                        {
                            string childThrfitType = child.getThrfitType();
                            doProtocolMethod(oprot, childThrfitType, obj, WRITE);
                        }
                    }
                    else
                    {
                        foreach (object obj in list)
                        {
                            short ids = 1;
                            writeVal(oprot, child, obj, ids);
                        }
                    }
                    oprot.WriteListEnd();
                }

                oprot.WriteFieldEnd();
            }
            else
            {
                oprot.IncrementRecursionDepth();
                try
                {
                    TField field = new TField();
                    field.Name = genericTree.getName();
                    field.Type = TType.Struct;
                    field.ID   = id;
                    oprot.WriteFieldBegin(field);

                    String  structName  = genericTree.getType();
                    TStruct STRUCT_DESC = new TStruct(structName);
                    oprot.WriteStructBegin(STRUCT_DESC);

                    //TODO:这个map是有序的
                    Dictionary <String, GenericTree> children = genericTree.getChildren();

                    Dictionary <string, object> map = value as Dictionary <string, object>;

                    short ids = 1;
                    foreach (GenericTree child in children.Values)
                    {
                        //if (map.ContainsKey(child.getName()))
                        //{
                        writeVal(oprot, child, map[child.getName()], ids++);
                        //}
                    }
                    oprot.WriteFieldStop();
                    oprot.WriteStructEnd();

                    oprot.WriteFieldEnd();
                }
                finally
                {
                    oprot.DecrementRecursionDepth();
                }
            }
        }