当前位置: 首页>>代码示例>>C#>>正文


C# Encoder.WriteFloat方法代码示例

本文整理汇总了C#中Encoder.WriteFloat方法的典型用法代码示例。如果您正苦于以下问题:C# Encoder.WriteFloat方法的具体用法?C# Encoder.WriteFloat怎么用?C# Encoder.WriteFloat使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Encoder的用法示例。


在下文中一共展示了Encoder.WriteFloat方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: EncodeDefaultValue

        /// <summary>
        /// Reads the passed JToken default value field and writes it in the specified encoder 
        /// </summary>
        /// <param name="enc">encoder to use for writing</param>
        /// <param name="schema">schema object for the current field</param>
        /// <param name="jtok">default value as JToken</param>
        public static void EncodeDefaultValue(Encoder enc, Schema schema, JToken jtok)
        {
            if (null == jtok) return;

            switch (schema.Tag)
            {
                case Schema.Type.Boolean:
                    if (jtok.Type != JTokenType.Boolean)
                        throw new AvroException("Default boolean value " + jtok.ToString() + " is invalid, expected is json boolean.");
                    enc.WriteBoolean((bool)jtok);
                    break;

                case Schema.Type.Int:
                    if (jtok.Type != JTokenType.Integer)
                        throw new AvroException("Default int value " + jtok.ToString() + " is invalid, expected is json integer.");
                    enc.WriteInt(Convert.ToInt32((int)jtok));
                    break;

                case Schema.Type.Long:
                    if (jtok.Type != JTokenType.Integer)
                        throw new AvroException("Default long value " + jtok.ToString() + " is invalid, expected is json integer.");
                    enc.WriteLong(Convert.ToInt64((long)jtok));
                    break;

                case Schema.Type.Float:
                    if (jtok.Type != JTokenType.Float)
                        throw new AvroException("Default float value " + jtok.ToString() + " is invalid, expected is json number.");
                    enc.WriteFloat((float)jtok);
                    break;

                case Schema.Type.Double:
                    if (jtok.Type == JTokenType.Integer)
                        enc.WriteDouble(Convert.ToDouble((int)jtok));
                    else if (jtok.Type == JTokenType.Float)
                        enc.WriteDouble(Convert.ToDouble((float)jtok));
                    else
                        throw new AvroException("Default double value " + jtok.ToString() + " is invalid, expected is json number.");

                    break;

                case Schema.Type.Bytes:
                    if (jtok.Type != JTokenType.String)
                        throw new AvroException("Default bytes value " + jtok.ToString() + " is invalid, expected is json string.");
                    var en = System.Text.Encoding.GetEncoding("iso-8859-1");
                    enc.WriteBytes(en.GetBytes((string)jtok));
                    break;

                case Schema.Type.Fixed:
                    if (jtok.Type != JTokenType.String)
                        throw new AvroException("Default fixed value " + jtok.ToString() + " is invalid, expected is json string.");
                    en = System.Text.Encoding.GetEncoding("iso-8859-1");
                    int len = (schema as FixedSchema).Size;
                    byte[] bb = en.GetBytes((string)jtok);
                    if (bb.Length != len)
                        throw new AvroException("Default fixed value " + jtok.ToString() + " is not of expected length " + len);
                    enc.WriteFixed(bb);
                    break;

                case Schema.Type.String:
                    if (jtok.Type != JTokenType.String)
                        throw new AvroException("Default string value " + jtok.ToString() + " is invalid, expected is json string.");
                    enc.WriteString((string)jtok);
                    break;

                case Schema.Type.Enumeration:
                    if (jtok.Type != JTokenType.String)
                        throw new AvroException("Default enum value " + jtok.ToString() + " is invalid, expected is json string.");
                    enc.WriteEnum((schema as EnumSchema).Ordinal((string)jtok));
                    break;

                case Schema.Type.Null:
                    if (jtok.Type != JTokenType.Null)
                        throw new AvroException("Default null value " + jtok.ToString() + " is invalid, expected is json null.");
                    enc.WriteNull();
                    break;

                case Schema.Type.Array:
                    if (jtok.Type != JTokenType.Array)
                        throw new AvroException("Default array value " + jtok.ToString() + " is invalid, expected is json array.");
                    JArray jarr = jtok as JArray;
                    enc.WriteArrayStart();
                    enc.SetItemCount(jarr.Count);
                    foreach (JToken jitem in jarr)
                    {
                        enc.StartItem();
                        EncodeDefaultValue(enc, (schema as ArraySchema).ItemSchema, jitem);
                    }
                    enc.WriteArrayEnd();
                    break;

                case Schema.Type.Record:
                case Schema.Type.Error:
                    if (jtok.Type != JTokenType.Object)
                        throw new AvroException("Default record value " + jtok.ToString() + " is invalid, expected is json object.");
//.........这里部分代码省略.........
开发者ID:sdgdsffdsfff,项目名称:hermes.net,代码行数:101,代码来源:Resolver.cs


注:本文中的Encoder.WriteFloat方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。