Esempio n. 1
0
        // private methods
        private void AssertSerializesTheSame(Newtonsoft.Json.Linq.JValue value, GuidRepresentation guidRepresentation = GuidRepresentation.Unspecified)
        {
            var subject = CreateSubject();

            var result = Serialize(subject, value, mustBeNested: true, guidRepresentation: guidRepresentation);

            var expectedResult = SerializeUsingNewtonsoftWriter(value, mustBeNested: true);

            result.Should().Equal(expectedResult);
        }
        public void Deserialize_should_return_expected_result_for_BsonJavaScriptWithScope(string type, string json)
        {
            var subject = CreateSubject();
            var bson = ToBson(json);

            var result = Deserialize(subject, bson, mustBeNested: true);

            // note: native reader throws, our reader adapter returns the code and drops the scope
            var expectedResult = new Newtonsoft.Json.Linq.JValue("abc");
            result.Should().Be(expectedResult);
        }
Esempio n. 3
0
        //Creating JArray
        public Newtonsoft.Json.Linq.JArray GetJArray2()
        {
            Newtonsoft.Json.Linq.JArray array = new Newtonsoft.Json.Linq.JArray();
            Newtonsoft.Json.Linq.JValue text  = new Newtonsoft.Json.Linq.JValue("Manual text");
            Newtonsoft.Json.Linq.JValue date  = new Newtonsoft.Json.Linq.JValue(new DateTime(2000, 5, 23));
            //add to JArray
            array.Add(text);
            array.Add(date);

            return(array);
        }
Esempio n. 4
0
        public void Deserialize_should_return_expected_result_for_BsonJavaScriptWithScope(string type, string json)
        {
            var subject = CreateSubject();
            var bson    = ToBson(json);

            var result = Deserialize(subject, bson, mustBeNested: true);

            // note: native reader throws, our reader adapter returns the code and drops the scope
            var expectedResult = new Newtonsoft.Json.Linq.JValue("abc");

            result.Should().Be(expectedResult);
        }
Esempio n. 5
0
        public static void PopulateUserFromJson(User obj, Newtonsoft.Json.Linq.JObject json)
        {
            funapi.Log.Assert(json["Id"] == null);

            if (json["WinCount"] != null)
            {
                funapi.Log.Assert(json["WinCount"].Type == Newtonsoft.Json.Linq.JTokenType.Integer);
                Newtonsoft.Json.Linq.JValue win_count = json["WinCount"] as Newtonsoft.Json.Linq.JValue;
                obj.SetWinCount((long)win_count);
            }

            if (json["LoseCount"] != null)
            {
                funapi.Log.Assert(json["LoseCount"].Type == Newtonsoft.Json.Linq.JTokenType.Integer);
                Newtonsoft.Json.Linq.JValue lose_count = json["LoseCount"] as Newtonsoft.Json.Linq.JValue;
                obj.SetLoseCount((long)lose_count);
            }

            if (json["WinningStreak"] != null)
            {
                funapi.Log.Assert(json["WinningStreak"].Type == Newtonsoft.Json.Linq.JTokenType.Integer);
                Newtonsoft.Json.Linq.JValue winning_streak = json["WinningStreak"] as Newtonsoft.Json.Linq.JValue;
                obj.SetWinningStreak((long)winning_streak);
            }

            if (json["WinningStreakDayOfYear"] != null)
            {
                funapi.Log.Assert(json["WinningStreakDayOfYear"].Type == Newtonsoft.Json.Linq.JTokenType.Integer);
                Newtonsoft.Json.Linq.JValue winning_streakday_ofyear = json["WinningStreakDayOfYear"] as Newtonsoft.Json.Linq.JValue;
                obj.SetWinningStreakDayOfYear((long)winning_streakday_ofyear);
            }

            if (json["WinCountSingle"] != null)
            {
                funapi.Log.Assert(json["WinCountSingle"].Type == Newtonsoft.Json.Linq.JTokenType.Integer);
                Newtonsoft.Json.Linq.JValue win_countsingle = json["WinCountSingle"] as Newtonsoft.Json.Linq.JValue;
                obj.SetWinCountSingle((long)win_countsingle);
            }

            if (json["LoseCountSingle"] != null)
            {
                funapi.Log.Assert(json["LoseCountSingle"].Type == Newtonsoft.Json.Linq.JTokenType.Integer);
                Newtonsoft.Json.Linq.JValue lose_countsingle = json["LoseCountSingle"] as Newtonsoft.Json.Linq.JValue;
                obj.SetLoseCountSingle((long)lose_countsingle);
            }

            if (json["WinningStreakSingle"] != null)
            {
                funapi.Log.Assert(json["WinningStreakSingle"].Type == Newtonsoft.Json.Linq.JTokenType.Integer);
                Newtonsoft.Json.Linq.JValue winning_streaksingle = json["WinningStreakSingle"] as Newtonsoft.Json.Linq.JValue;
                obj.SetWinningStreakSingle((long)winning_streaksingle);
            }
        }
Esempio n. 6
0
        // private methods
        private void AssertSerializesTheSame(Newtonsoft.Json.Linq.JValue value, GuidRepresentation guidRepresentation = GuidRepresentation.Unspecified)
        {
            var subject = CreateSubject();

            var result = Serialize(subject, value, mustBeNested: true, guidRepresentation: guidRepresentation);

            var expectedResult = SerializeUsingNewtonsoftWriter(value, mustBeNested: true, guidRepresentation: guidRepresentation);

            var resultAsString   = string.Join(", ", result.Select(b => $"{b:X2}"));
            var expectedAsString = string.Join(", ", expectedResult.Select(b => $"{b:X2}"));

            resultAsString.Should().BeEquivalentTo(expectedAsString);
        }
Esempio n. 7
0
        //为了简化通讯协议,TCP用bson,http用JSON
        //Newtonsoft.Json 和  Newtonsoft.Json.Bson
        static void Main(string[] args)
        {
            Newtonsoft.Json.Linq.JValue  v   = new Newtonsoft.Json.Linq.JValue(11.0);
            Newtonsoft.Json.Linq.JObject obj = new Newtonsoft.Json.Linq.JObject();
            obj["abc"] = v;
            var stream = new System.IO.MemoryStream();

            Newtonsoft.Json.Bson.BsonDataWriter w = new Newtonsoft.Json.Bson.BsonDataWriter(stream);
            obj.WriteTo(w);
            var bts = stream.ToArray();

            Newtonsoft.Json.Bson.BsonDataReader r = new Newtonsoft.Json.Bson.BsonDataReader(new System.IO.MemoryStream(bts));
            var token = Newtonsoft.Json.Linq.JToken.ReadFrom(r);

            Console.WriteLine("Hello World!");

            var tree = Microsoft.CodeAnalysis.CSharp.CSharpSyntaxTree.ParseText("class A{public  int aaa(){return 3+4;}}");

            var op   = new CSharpCompilationOptions(Microsoft.CodeAnalysis.OutputKind.DynamicallyLinkedLibrary);
            var ref1 = MetadataReference.CreateFromFile("needlib" + System.IO.Path.DirectorySeparatorChar + "mscorlib.dll");
            var comp = Microsoft.CodeAnalysis.CSharp.CSharpCompilation.Create("aaa.dll", new[] { tree },
                                                                              new[] { ref1 }, op);
            var ms     = new System.IO.MemoryStream();
            var mspdb  = new System.IO.MemoryStream();
            var result = comp.Emit(ms, mspdb);

            Console.WriteLine("result=" + result.Success);
            Console.WriteLine("dll=" + ms.Length);
            Console.WriteLine("pdb=" + mspdb.Length);
            System.IO.File.WriteAllBytes("111.dll", ms.ToArray());
            System.IO.File.WriteAllBytes("111.pdb", mspdb.ToArray());
            foreach (var d in result.Diagnostics)
            {
                Console.WriteLine(d.Descriptor.Description.ToString());
                Console.WriteLine(d.Descriptor.Title.ToString());
                Console.WriteLine(d.Descriptor.Category);
                Console.WriteLine(d.Descriptor.MessageFormat.ToString());
            }
            Console.WriteLine("rosyln ok.");
        }
Esempio n. 8
0
        public dynamic changeStatusDriver([FromBody] Newtonsoft.Json.Linq.JValue param)
        {
            dynamic objresponse = null;
            dynamic obj         = param;
            int     DriverID    = obj;

            try
            {
                var dobj = _repositoryWrapper.Driver.FindByID(DriverID);
                if (dobj.status == 0)
                {
                    dobj.status = 1;
                }
                _repositoryWrapper.Driver.Update(dobj.status);
                return(objresponse);
            }
            catch (Exception ex)
            {
                _repositoryWrapper.EventLog.Error("", ex.Message);
            }
            return(objresponse);
        }
Esempio n. 9
0
        //为了简化通讯协议,TCP用bson,http用JSON
        //Newtonsoft.Json 和  Newtonsoft.Json.Bson
        static void Main(string[] args)
        {
            Newtonsoft.Json.Linq.JValue  v   = new Newtonsoft.Json.Linq.JValue(11.0);
            Newtonsoft.Json.Linq.JObject obj = new Newtonsoft.Json.Linq.JObject();
            obj["abc"] = v;
            var stream = new System.IO.MemoryStream();

            Newtonsoft.Json.Bson.BsonDataWriter w = new Newtonsoft.Json.Bson.BsonDataWriter(stream);
            obj.WriteTo(w);
            var bts = stream.ToArray();

            Newtonsoft.Json.Bson.BsonDataReader r = new Newtonsoft.Json.Bson.BsonDataReader(new System.IO.MemoryStream(bts));
            var token = Newtonsoft.Json.Linq.JToken.ReadFrom(r);

            Console.WriteLine("Hello World!");

            var tree = Microsoft.CodeAnalysis.CSharp.CSharpSyntaxTree.ParseText("class A{public  int aaa(){return 3;}}");

            var op   = new CSharpCompilationOptions(Microsoft.CodeAnalysis.OutputKind.DynamicallyLinkedLibrary);
            var ref1 = MetadataReference.CreateFromFile("needlib\\mscorlib.dll");
            var comp = Microsoft.CodeAnalysis.CSharp.CSharpCompilation.Create("aaa.dll", new[] { tree },
                                                                              new[] { ref1 }, op);
            var result = comp.Emit("e:\\111.dll", pdbPath: "e:\\111.pdb");
        }
Esempio n. 10
0
        public void Serialize_value_should_have_expected_result(string type, object objectValue, string _expectedResult)
        {
            var value = new Newtonsoft.Json.Linq.JValue(objectValue);

            AssertSerializesTheSame(value);
        }
Esempio n. 11
0
        public void Serialize_Guid_should_have_expected_result(string stringValue, GuidRepresentation guidRepresentation, string expectedResult)
        {
            var value = new Newtonsoft.Json.Linq.JValue(Guid.Parse(stringValue));

            AssertSerializesTheSame(value, guidRepresentation);
        }
Esempio n. 12
0
        public void Serialize_decimal_should_have_expected_result(string stringValue, string expectedResult)
        {
            var value = new Newtonsoft.Json.Linq.JValue(decimal.Parse(stringValue, System.Globalization.CultureInfo.InvariantCulture));

            AssertSerializesTheSame(value);
        }
Esempio n. 13
0
        internal void OnEthServerPacket(byte[] buffer, int length)
        {
            try
            {
                dynamic dyn = JsonConvert.DeserializeObject(Encoding.UTF8.GetString(buffer, 0, length));

                //Logger.LogToConsole("dyn.method: " + dyn.method);

                if (dyn.id != null)
                {
                    switch ((int)dyn.id)
                    {
                    case 2:     //Login authorize
                        if ((bool)dyn.result)
                        {
                            Logger.LogToConsole("Stratum Authorization success: " + redirector.thisMiner.displayName, redirector.thisMiner.endPoint, ConsoleColor.DarkGreen);
                        }
                        else
                        {
                            Logger.LogToConsole("Stratum Authorization failure: " + redirector.thisMiner.displayName, redirector.thisMiner.endPoint, ConsoleColor.Red);
                        }
                        break;

                        /*case 4: //Share
                         *  if ((bool)dyn.result)
                         *  {
                         *      redirector.AcceptedShare();
                         *      Logger.LogToConsole(string.Format(redirector.thisMiner.displayName + "'s share got accepted. [{0} shares accepted]", redirector.thisMiner.acceptedShares), redirector.thisMiner.endPoint, ConsoleColor.Green);
                         *  }
                         *  else
                         *  {
                         *      redirector.RejectedShare();
                         *      Logger.LogToConsole(string.Format(redirector.thisMiner.displayName + "'s share got rejected. [{0} shares rejected]", redirector.thisMiner.rejectedShares), redirector.thisMiner.endPoint, ConsoleColor.Red);
                         *  }
                         *  break;*/
                    }

                    if (/*(int)dyn.id != 4 && */ (int)dyn.id == lastShareSubmitID) // Testin
                    {
                        if (dyn.result != null)
                        {
                            if ((bool)dyn.result)
                            {
                                redirector.AcceptedShare();
                                Logger.LogToConsole(string.Format(redirector.thisMiner.displayName + "'s share got accepted. [{0} shares accepted]", redirector.thisMiner.acceptedShares), redirector.thisMiner.endPoint, ConsoleColor.Green);
                            }
                            else
                            {
                                redirector.RejectedShare();
                                Logger.LogToConsole(string.Format(redirector.thisMiner.displayName + "'s share got rejected. [{0} shares rejected]", redirector.thisMiner.rejectedShares), redirector.thisMiner.endPoint, ConsoleColor.Red);
                            }
                            //lastShareSubmitID = -1;
                        }
                    }
                }

                if (dyn.method != null)
                {
                    switch ((string)dyn.method)
                    {
                    case "mining.notify":
                        if (Program.settings.debug)
                        {
                            Logger.LogToConsole(string.Format(redirector.thisMiner.displayName + " got a job"), redirector.thisMiner.endPoint);
                        }
                        break;

                    case "mining.set_difficulty":
                        Newtonsoft.Json.Linq.JValue val = dyn.@params[0];
                        string diff = val.Value.ToString();
                        Logger.LogToConsole(string.Format("Pool set difficulty to: " + diff), redirector.thisMiner.endPoint);
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.LogToConsole(ex.ToString(), redirector.thisMiner.endPoint);
                if (Program.settings.debug)
                {
                    Logger.LogToConsole("Json Err: " + Encoding.UTF8.GetString(buffer, 0, length), redirector.thisMiner.endPoint, ConsoleColor.Red);
                }
            }

            if (redirector.thisMiner.connectionAlive && redirector.m_client.Disposed == false)
            {
                redirector.m_client.Send(buffer, length);
            }
        }
Esempio n. 14
0
        public void Serialize_DateTime_should_have_expected_result(string stringValue, string expectedResult)
        {
            var value = new Newtonsoft.Json.Linq.JValue(DateTime.Parse(stringValue));

            AssertSerializesTheSame(value);
        }
        public void Serialize_value_should_have_expected_result(string type, object objectValue, string _expectedResult)
        {
            var value = new Newtonsoft.Json.Linq.JValue(objectValue);

            AssertSerializesTheSame(value);
        }
        public void Serialize_Uri_should_have_expected_result(string stringValue, string expectedResult)
        {
            var value = new Newtonsoft.Json.Linq.JValue(new Uri(stringValue));

            AssertSerializesTheSame(value);
        }
        public void Serialize_Guid_should_have_expected_result(string stringValue, GuidRepresentation guidRepresentation, string expectedResult)
        {
            var value = new Newtonsoft.Json.Linq.JValue(Guid.Parse(stringValue));

            AssertSerializesTheSame(value);
        }
Esempio n. 18
0
        public void Serialize_char_should_have_expected_result(char charValue, string expectedResult)
        {
            var value = new Newtonsoft.Json.Linq.JValue(charValue);

            AssertSerializesTheSame(value);
        }
        public void Serialize_char_should_have_expected_result(char charValue, string expectedResult)
        {
            var value = new Newtonsoft.Json.Linq.JValue(charValue);

            AssertSerializesTheSame(value);
        }
Esempio n. 20
0
        internal void OnZcashClientPacket(byte[] buffer, int length)
        {
            bool madeChanges = false;

            byte[] newBuffer = null;
            int    newLength = 0;

            try
            {
                dynamic dyn = JsonConvert.DeserializeObject(Encoding.UTF8.GetString(buffer, 0, length));

                if (dyn.method != null)
                {
                    switch ((string)dyn.method)
                    {
                    case "mining.authorize":
                        if (dyn.@params != null)
                        {
                            Logger.LogToConsole("Zcash authorize detected!", redirector.thisMiner.endPoint, ConsoleColor.DarkGreen);
                            madeChanges = true;

                            Newtonsoft.Json.Linq.JValue val = dyn.@params[0];
                            string wallet = val.Value.ToString();

                            if (wallet.Contains(".") && Program.settings.useDotWithRigName)
                            {    //There is likely a rigName in the wallet address.
                                redirector.thisMiner.replacedWallet = wallet;
                                redirector.thisMiner.rigName        = wallet.Substring(wallet.IndexOf(".") + 1);
                                redirector.thisMiner.displayName    = redirector.thisMiner.rigName;
                                if (Program.settings.replaceWallet)
                                {
                                    dyn.@params[0] = Program.settings.walletAddress + "." + redirector.thisMiner.rigName;
                                }
                            }
                            else if (wallet.Contains("/") && Program.settings.useSlashWithRigName)
                            {    //There is likely different rigname, may need to check for email addresses here as well
                                redirector.thisMiner.replacedWallet = wallet;
                                redirector.thisMiner.rigName        = wallet.Substring(wallet.IndexOf("/") + 1);
                                redirector.thisMiner.displayName    = redirector.thisMiner.rigName;
                                if (Program.settings.replaceWallet)
                                {
                                    dyn.@params[0] = Program.settings.walletAddress + "/" + redirector.thisMiner.rigName;
                                }
                            }
                            else if (Program.settings.identifyDevFee)
                            {    //there is no rigName, so we just replace the wallet
                                redirector.thisMiner.replacedWallet = wallet;

                                if (redirector.thisMiner.replacedWallet != Program.settings.walletAddress)
                                {
                                    redirector.thisMiner.displayName = "DevFee";
                                }

                                if (Program.settings.replaceWallet)
                                {
                                    dyn.@params[0] = Program.settings.walletAddress + "." + redirector.thisMiner.displayName;
                                }
                            }
                            else
                            {
                                if (Program.settings.replaceWallet)
                                {
                                    dyn.@params[0] = Program.settings.walletAddress;
                                }
                                if (Program.settings.debug)
                                {
                                    Logger.LogToConsole(string.Format("Worker: {0}", redirector.thisMiner.workerName));
                                }
                            }

                            string tempBuffer = JsonConvert.SerializeObject(dyn, Formatting.None) + "\n";

                            val    = dyn.@params[0];
                            wallet = val.Value.ToString();

                            if (Program.settings.replaceWallet)
                            {
                                lock (Logger.ConsoleBlockLock)
                                {
                                    Logger.LogToConsole("Old Wallet: " + redirector.thisMiner.replacedWallet, redirector.thisMiner.endPoint, ConsoleColor.Yellow);
                                    Logger.LogToConsole("New Wallet: " + wallet, redirector.thisMiner.endPoint, ConsoleColor.Yellow);
                                }
                            }
                            else
                            {
                                Logger.LogToConsole(string.Format("Wallet for {0}: {1}", redirector.thisMiner.displayName, wallet));
                            }

                            redirector.SetupMinerStats();

                            newBuffer = Encoding.UTF8.GetBytes(tempBuffer);
                            newLength = tempBuffer.Length;
                        }
                        break;

                    case "mining.submit":
                        if (Program.settings.replaceWallet)
                        {
                            madeChanges = true;

                            if (redirector.thisMiner.displayName.Length > 0)
                            {
                                dyn.@params[0] = Program.settings.walletAddress + "." + redirector.thisMiner.displayName;
                            }
                            else
                            {
                                dyn.@params[0] = Program.settings.walletAddress;
                            }

                            string tempBuffer = JsonConvert.SerializeObject(dyn, Formatting.None) + "\n";

                            newBuffer = Encoding.UTF8.GetBytes(tempBuffer);
                            newLength = tempBuffer.Length;

                            Newtonsoft.Json.Linq.JValue val = dyn.@params[0];
                            string wallet = val.Value.ToString();

                            if (Program.settings.debug)
                            {
                                lock (Logger.ConsoleBlockLock)
                                {
                                    Logger.LogToConsole("Old Wallet: " + redirector.thisMiner.replacedWallet, redirector.thisMiner.endPoint, ConsoleColor.Yellow);
                                    Logger.LogToConsole("New Wallet: " + wallet, redirector.thisMiner.endPoint, ConsoleColor.Yellow);
                                }
                            }
                        }

                        redirector.SubmittedShare();
                        Logger.LogToConsole(string.Format(redirector.thisMiner.displayName + " found a share. [{0} shares found]", redirector.thisMiner.submittedShares), redirector.thisMiner.endPoint, ConsoleColor.Green);
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                madeChanges = false;
                Logger.LogToConsole(ex.ToString(), redirector.thisMiner.endPoint);
                if (Program.settings.debug)
                {
                    Logger.LogToConsole("Json Err: " + Encoding.UTF8.GetString(buffer, 0, length), redirector.thisMiner.endPoint);
                }
            }

            if (redirector.thisMiner.connectionAlive && redirector.m_server.Disposed == false)
            {
                if (!madeChanges)
                {
                    redirector.m_server.Send(buffer, length);
                }
                else
                {
                    redirector.m_server.Send(newBuffer, newLength);
                }
            }
        }
        public void Serialize_DateTimeOffset_should_have_expected_result(string stringValue, string expectedResult)
        {
            var value = new Newtonsoft.Json.Linq.JValue(DateTimeOffset.Parse(stringValue));

            AssertSerializesTheSame(value);
        }