/// <summary> /// Remove all the fields with the given name and add a single new field with the given name and new object value. /// </summary> /// <remarks> /// <para>This is equivalent to calling RemoveAll(name); and then Add(name,obj);.</para> /// <para>A check of the given type is performed before the remove method is called to throw an exception before removing any data.</para> /// </remarks> /// <param name="name">Name of the field to replace and then add.</param> /// <param name="obj">The value of the object to set in the new field.</param> /// <exception cref="ArgumetException">if the given object is not of a supported type.</exception>" public void ReplaceAll(string name, object obj) { // check if the type is supported first DataField.GetType(obj); RemoveAll(name); Add(name, obj); }
public virtual byte[] Encode(object obj) { object[] ary = (object[])obj; MemoryStream ms = new MemoryStream(); BinaryWriter bw = new BinaryWriter(ms); for (int x = 0; x < ary.Length; x++) { try { // This will throw an exception for any unsupported data types. byte tipe = DataField.GetType(ary[x]); byte[] data = DataField.Encode(ary[x], tipe); int size = DataField.GetDataType(tipe).Size; // Write the type field bw.Write(tipe); if (data != null) { // If the value is variable in length if (size < 0) { // write the length as a u32 like ither variable field length types bw.Write(ByteUtil.RenderUnsignedInt((uint)data.Length)); } // write the value itself bw.Write(data); } else { bw.Write(0); // null value } } catch (System.Exception) { //System.err.println("Array object of type " + ary[x].getClass().getSimpleName() + " is not supported in DataFrames"); // just skip the offending object and add the rest. } } // for each return(ms.ToArray()); }
/// <summary> /// Convert the object into a binary representation of a DataField /// </summary> /// <param name="obj">The object to encode.</param> /// <returns>The bytes representing the object in DataFrame format.</returns> /// <exception cref="ArgumentException">if the object cannot be encoded - type not supported</exception> public static byte[] Encode(object obj) { return(DataField.Encode(obj, DataField.GetType(obj))); }
/// <summary> /// The constructor for the most common use case, Name-Value pair /// </summary> /// <param name="name">The name of this DataField</param> /// <param name="obj">The object value to encode</param> /// <exception cref="ArgumentException">If the name is not valid or the object type is not supported</exception> public DataField(string name, object obj) { this.name = DataField.NameCheck(name); type = DataField.GetType(obj); value = DataField.Encode(obj, type); }
/// <summary> /// Create a DataField for the specific object. /// </summary> /// <param name="obj">The object to use as the value of the field</param> /// <exception cref="ArgumentException">If the object type is not supported</exception> public DataField(object obj) { type = DataField.GetType(obj); value = DataField.Encode(obj, type); }