Represents a Python long int object. See the documentation at http://www.python.org/doc/current/api/longObjects.html
Inheritance: PyNumber
示例#1
0
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            if (value == null)
            {
                serializer.Serialize(writer, null);
                return;
            }

            var jobj      = (PyObject)value;
            var jojb_type = jobj.GetPythonType().ToString();

            if (jojb_type == "<class 'NoneType'>")
            {
                writer.WriteNull();
            }

            else if (PyDict.IsDictType(jobj))
            {
                var dict = new PyDict(jobj);
                var keys = dict.Keys();

                writer.WriteStartObject();

                foreach (PyObject key in keys)
                {
                    string name = key.ToString();
                    var    val  = jobj[key];//.ToString();

                    writer.WritePropertyName(name);
                    serializer.Serialize(writer, val, null);
                }
                writer.WriteEndObject();
            }
            else if (PyLong.IsLongType(jobj))
            {
                var pobj = new PyLong(jobj);
                writer.WriteValue(pobj.ToInt64());
            }
            else if (PyInt.IsIntType(jobj))
            {
                var pobj = new PyInt(jobj);
                writer.WriteValue(pobj.ToInt32());
            }
            else if (PyFloat.IsFloatType(jobj))
            {
                var pobj = new PyFloat(jobj);
                writer.WriteValue(pobj.ToDouble());
            }

            else if (PyString.IsStringType(jobj) || jojb_type == "<class 'datetime.date'>")
            {
                // var pobj = new PyString(jobj);
                writer.WriteValue(jobj.ToString());
            }
            else if (jobj.IsIterable())// && !PyDict.IsDictType(jobj))
            {
                writer.WriteStartArray();
                foreach (var element in jobj)
                {
                    serializer.Serialize(writer, element, null);
                }
                writer.WriteEndArray();
            }
            else
            {
                var properties = jobj.Dir();
                if (properties != null)
                {
                    writer.WriteStartObject();

                    foreach (PyObject property in properties)
                    {
                        string name = property.ToString();
                        if (!property.IsCallable() && !name.StartsWith("__"))
                        {
                            var attr = jobj.GetAttr(property);//.ToString();
                            writer.WritePropertyName(name);
                            try
                            {
                                serializer.Serialize(writer, attr, null);
                            }
                            catch
                            {
                                writer.WriteNull();
                            }
                        }
                    }
                    writer.WriteEndObject();
                }
            }
        }
示例#2
0
 public void TestToInt64()
 {
     long largeNumber = 8L * 1024L * 1024L * 1024L; // 8 GB
     PyLong pyLargeNumber = new PyLong(largeNumber);
     Assert.AreEqual(largeNumber, pyLargeNumber.ToInt64());
 }