diff --git a/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerCollectionsTests.cs b/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerCollectionsTests.cs index c1008172..bee814ff 100644 --- a/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerCollectionsTests.cs +++ b/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerCollectionsTests.cs @@ -53,7 +53,9 @@ using Test = Xunit.FactAttribute; using Assert = Newtonsoft.Json.Tests.XUnitAssert; #else using NUnit.Framework; - +#endif +#if !NET20 && !PORTABLE40 +using System.Xml.Linq; #endif namespace Newtonsoft.Json.Tests.Serialization @@ -1930,8 +1932,256 @@ namespace Newtonsoft.Json.Tests.Serialization JsonConvert.DeserializeObject>(json); }, "Unable to find a constructor to use for type Newtonsoft.Json.Tests.Serialization.ReadOnlyCollectionWithArrayArgument`1[System.Double]. Path '', line 1, position 1."); } + +#if !NET20 && !PORTABLE40 + [Test] + public void NonDefaultConstructor_DuplicateKeyInDictionary_Replace() + { + string json = @"{ ""user"":""bpan"", ""Person"":{ ""groups"":""replaced!"", ""domain"":""adm"", ""mail"":""bpan@sdu.dk"", ""sn"":""Pan"", ""gn"":""Benzhi"", ""cn"":""Benzhi Pan"", ""eo"":""BQHLJaVTMr0eWsi1jaIut4Ls/pSuMeNEmsWfWsfKo="", ""guid"":""9A38CE8E5B288942A8DA415CF5E687"", ""employeenumber"":""2674"", ""omk1"":""930"", ""language"":""da"" }, ""XMLResponce"":""\n\n\t\n\t\tbpan\n\t\t\n\t\t\tFNC-PRI-APP-SUNDB-EDOR-A,FNC-RI-APP-SUB-EDITOR-B\n\t\t\tadm\n\t\t\tbpan@sdu.dk\n\t\t\tPan\n\t\t\tBenzhi\n\t\t\tBenzhi Pan\n\t\t\tBQHLJaVTMr0eWsi1jaIut4Lsfr/pSuMeNEmsWfWsfKo=\n\t\t\t9A38CE8E5B288942A8DA415C2C687\n\t\t\t274\n\t\t\t930\n\t\t\tda\n\t\t\n\t\n\n"", ""Language"":1, ""Groups"":[ ""FNC-PRI-APP-SNDB-EDOR-A"", ""FNC-PI-APP-SUNDB-EDOR-B"" ], ""Domain"":""adm"", ""Mail"":""bpan@sdu.dk"", ""Surname"":""Pan"", ""Givenname"":""Benzhi"", ""CommonName"":""Benzhi Pan"", ""OrganizationName"":null }"; + + var result = JsonConvert.DeserializeObject(json); + + Assert.AreEqual("replaced!", result.Person["groups"]); + } +#endif } +#if !NET20 && !PORTABLE40 + public class CASResponce + { + // + // + // + // and + // + // IT-service-OD,USR-IT-service,IT-service-udvikling + // adm + // and@sdu.dk + // And + // Anders + // Anders And + // QQT3tKSKjCxQSGsDiR8HTP9L5VsojBvOYyjOu8pwLMA= + // DE423352CC763649B8F2ECF1DA304750 + // da + // + // + // + + // NemID + // + // + // + // 2903851921 + // + // + // + + + //WAYF + // + // + // + // jj@testidp.wayf.dk + // + // + // Jensen + // Jens + // Jens farmer + // jj @testidp.wayf.dk + // jens.jensen @institution.dk + // Institution + // 2 + // urn:mace:terena.org:schac:personalUniqueID:dk:CPR:0708741234 + // student @course1.testidp.wayf.dk + // staff @course1.testidp.wayf.dk + // staff @course1.testidp.wsayf.dk + // en + // test + // student + // DK + // WAYF-DK-7a86d1c3b69a9639d7650b64f2eb773bd21a8c6d + // testidp.wayf.dk + // Jens + // Institution + // https://testbridge.wayf.dk + // + // + // + + + public enum ssoLanguage + { + Unknown, + Danish, + English + } + + + public CASResponce(string xmlResponce) + { + this.Domain = ""; + this.Mail = ""; + this.Surname = ""; + this.Givenname = ""; + this.CommonName = ""; + + ParseReplyXML(xmlResponce); + ExtractGroups(); + ExtractLanguage(); + } + + private void ExtractGroups() + { + this.Groups = new List(); + if (this.Person.ContainsKey("groups")) + { + string groupsString = this.Person["groups"]; + string[] stringList = groupsString.Split(','); + + foreach (string group in stringList) + { + this.Groups.Add(group); + } + } + + } + + private void ExtractLanguage() + { + if (Person.ContainsKey("language")) + { + switch (Person["language"].Trim()) + { + case "da": + this.Language = ssoLanguage.Danish; + break; + case "en": + this.Language = ssoLanguage.English; + break; + default: + this.Language = ssoLanguage.Unknown; + break; + } + } + else + { + this.Language = ssoLanguage.Unknown; + } + } + + + + + private void ParseReplyXML(string xmlString) + { + try + { + System.Xml.Linq.XDocument xDoc = XDocument.Parse(xmlString); + + var root = xDoc.Root; + + string ns = "http://www.yale.edu/tp/cas"; + + XElement auth = root.Element(XName.Get("authenticationSuccess", ns)); + + if (auth == null) + auth = root.Element(XName.Get("authenticationFailure", ns)); + + XElement xNodeUser = auth.Element(XName.Get("user", ns)); + + XElement eduPers = auth.Element(XName.Get("norEduPerson", "")); + + string casUser = ""; + Dictionary eduPerson = new Dictionary(); + + if (xNodeUser != null) + { + casUser = xNodeUser.Value; + + if (eduPers != null) + { + foreach (XElement xPersonValue in eduPers.Elements()) + { + if (!eduPerson.ContainsKey(xPersonValue.Name.LocalName)) + { + eduPerson.Add(xPersonValue.Name.LocalName, xPersonValue.Value); + } + else + { + eduPerson[xPersonValue.Name.LocalName] = eduPerson[xPersonValue.Name.LocalName] + ";" + xPersonValue.Value; + } + } + } + } + + if (casUser.Trim() != "") + { + this.user = casUser; + } + + if (eduPerson.ContainsKey("domain")) + this.Domain = eduPerson["domain"]; + if (eduPerson.ContainsKey("organizationName")) + this.OrganizationName = eduPerson["organizationName"]; + if (eduPerson.ContainsKey("mail")) + this.Mail = eduPerson["mail"]; + if (eduPerson.ContainsKey("sn")) + this.Surname = eduPerson["sn"]; + if (eduPerson.ContainsKey("gn")) + this.Givenname = eduPerson["gn"]; + if (eduPerson.ContainsKey("cn")) + this.CommonName = eduPerson["cn"]; + + this.Person = eduPerson; + this.XMLResponce = xmlString; + } + catch + { + this.user = ""; + + } + } + + /// + /// Fast felt der altid findes. + /// + public string user { get; private set; } + + /// + /// Person type som dictionary indeholdende de ekstra informationer returneret ved login. + /// + public Dictionary Person { get; private set; } + + /// + /// Den oprindelige xml returneret fra CAS. + /// + public string XMLResponce { get; private set; } + + /// + /// Det sprog der benyttes i SSO. Muligheder er da eller en. + /// + public ssoLanguage Language { get; private set; } + + /// + /// Liste af grupper som man er medlem af. Kun udvalgt iblandt dem der blev puttet ind i systemet. + /// + public List Groups { get; private set; } + + public string Domain { get; private set; } + + public string Mail { get; private set; } + + public string Surname { get; private set; } + + public string Givenname { get; private set; } + + public string CommonName { get; private set; } + + public string OrganizationName { get; private set; } + + } +#endif + public class ReadOnlyCollectionWithArrayArgument : IList { private readonly IList _values; diff --git a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs index 91ca7233..36f0e9fa 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs @@ -2046,7 +2046,7 @@ namespace Newtonsoft.Json.Serialization while (e.MoveNext()) { DictionaryEntry entry = e.Entry; - targetDictionary.Add(entry.Key, entry.Value); + targetDictionary[entry.Key] = entry.Value; } } finally