本文整理汇总了C#中Encoder.WriteInt方法的典型用法代码示例。如果您正苦于以下问题:C# Encoder.WriteInt方法的具体用法?C# Encoder.WriteInt怎么用?C# Encoder.WriteInt使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Encoder
的用法示例。
在下文中一共展示了Encoder.WriteInt方法的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.");
//.........这里部分代码省略.........