public void Example()
        {
            #region Usage
            JObject o = JObject.Parse(@"{
              'name': 'Bill G',
              'age': 58,
              'country': 'United States',
              'employer': 'Microsoft'
            }");

            o.AddAnnotation(new HashSet <string>());
            o.PropertyChanged += (sender, args) => o.Annotation <HashSet <string> >().Add(args.PropertyName);

            o["age"]      = 59;
            o["employer"] = "Bill & Melinda Gates Foundation";


            HashSet <string> changedProperties = o.Annotation <HashSet <string> >();
            // age
            // employer
            #endregion

            Assert.AreEqual(true, changedProperties.Contains("age"));
            Assert.AreEqual(true, changedProperties.Contains("employer"));
        }
        public void GetAnnotation_NeverSet()
        {
            JObject o = new JObject();

            string s = o.Annotation <string>();

            Assert.AreEqual(null, s);

            s = (string)o.Annotation(typeof(string));
            Assert.AreEqual(null, s);
        }
        public void AddAnnotation()
        {
            JObject o = new JObject();

            o.AddAnnotation("A string!");

            string s = o.Annotation <string>();

            Assert.AreEqual("A string!", s);

            s = (string)o.Annotation(typeof(string));
            Assert.AreEqual("A string!", s);
        }
        public void AddAnnotation_MultipleOfTheSameType()
        {
            JObject o = new JObject();

            o.AddAnnotation("A string!");
            o.AddAnnotation("Another string!");

            string s = o.Annotation <string>();

            Assert.AreEqual("A string!", s);

            s = (string)o.Annotation(typeof(string));
            Assert.AreEqual("A string!", s);
        }
        public void RemoveAnnotation_NonGeneric()
        {
            JObject o = new JObject();

            o.AddAnnotation("A string!");

            o.RemoveAnnotations(typeof(string));

            string s = o.Annotation <string>();

            Assert.AreEqual(null, s);

            s = (string)o.Annotation(typeof(string));
            Assert.AreEqual(null, s);
        }
Example #6
0
        private async Task <Guid> SendAsync(HttpMethod method, JObject entity, Guid entityId, string property)
        {
            string apiName    = entity.Annotation <string>();
            string requestUri = $"api/data/v{_apiVersion}/{apiName}" +
                                (entityId != Guid.Empty ? $"/({entityId.ToString()})" : string.Empty) +
                                (property != string.Empty ? $"/{property}" : string.Empty);

            var request = new HttpRequestMessage(method, requestUri)
            {
                Content = new StringContent(entity.ToString(), Encoding.UTF8, "application/json")
            };

            var response = await _httpClient.SendAsync(request);

            if (response.StatusCode != HttpStatusCode.NoContent)
            {
                // HTTP Code 204 = No Content expected, otherwise throw an exception
                throw new ApplicationException(response.Content.ToString());
            }

            // Return the Entity ID
            // For new entities, it is defined in the OData-EntityId header value
            // [Organization URI]/api/data/v8.2/accounts(00000000-0000-0000-0000-000000000001)
            return(entityId != Guid.Empty ? entityId : ReadEntityId(response));
        }
        public void RemoveAnnotation_MultipleWithDifferentTypes_NonGeneric()
        {
            JObject o = new JObject();

            o.AddAnnotation("A string!");
            o.AddAnnotation(new Uri("http://www.google.com/"));

            o.RemoveAnnotations(typeof(string));

            string s = o.Annotation <string>();

            Assert.AreEqual(null, s);

            Uri i = o.Annotation <Uri>();

            Assert.AreEqual(new Uri("http://www.google.com/"), i);
        }
        public void RemoveAnnotation_MultipleCalls()
        {
            JObject o = new JObject();

            o.AddAnnotation("A string!");
            o.AddAnnotation(new Uri("http://www.google.com/"));

            o.RemoveAnnotations <string>();
            o.RemoveAnnotations <Uri>();

            string s = o.Annotation <string>();

            Assert.AreEqual(null, s);

            Uri i = o.Annotation <Uri>();

            Assert.AreEqual(null, i);
        }
        public void MultipleAnnotationsAreCopied()
        {
            Version version = new Version(1, 2, 3, 4);

            JObject o = new JObject();

            o.AddAnnotation("string!");
            o.AddAnnotation(version);

            JObject o2 = (JObject)o.DeepClone();

            Assert.AreEqual("string!", o2.Annotation <string>());
            Assert.AreEqual(version, o2.Annotation <Version>());

            o2.RemoveAnnotations <Version>();
            Assert.AreEqual(1, o.Annotations <Version>().Count());
            Assert.AreEqual(0, o2.Annotations <Version>().Count());
        }
        public void AddAnnotation_MultipleOfDifferentTypes()
        {
            JObject o = new JObject();

            o.AddAnnotation("A string!");
            o.AddAnnotation(new Uri("http://www.google.com/"));

            string s = o.Annotation <string>();

            Assert.AreEqual("A string!", s);

            s = (string)o.Annotation(typeof(string));
            Assert.AreEqual("A string!", s);

            Uri i = o.Annotation <Uri>();

            Assert.AreEqual(new Uri("http://www.google.com/"), i);

            i = (Uri)o.Annotation(typeof(Uri));
            Assert.AreEqual(new Uri("http://www.google.com/"), i);
        }
        public void RemoveAnnotation()
        {
            JObject o = new JObject();

            o.AddAnnotation("A string!");

            o.RemoveAnnotations <string>();

            string s = o.Annotation <string>();

            Assert.AreEqual(null, s);
        }
        public static void ShowAnnotations()
        {
            Console.Clear();
            Console.WriteLine("*** Annotations ***");

            string courseJson = @"{
                                'name': 'Solr',
                                'secondsWatched': 0
                                }";
            //create
            JObject course = JObject.Parse(courseJson);

            course.AddAnnotation(new Dictionary<DateTime, int>());
            course.PropertyChanged += (sender, arguments) =>
            {
                DateTime annotationDate = DateTime.Now;
                Console.WriteLine("Adding new annotation at: " + annotationDate);

                course.Annotation<Dictionary<DateTime, int>>().Add(annotationDate,
                    ((JObject)sender)["secondsWatched"].Value<int>());
            };

            //Make a change
            course["secondsWatched"] = 10;
           
            //I showed you the event being raised, make another change
            course["secondsWatched"] = 150;

            //And another one
            course["secondsWatched"] = 250;

            Dictionary<DateTime, int> changesDone = course.Annotation<Dictionary<DateTime, int>>();
            foreach (KeyValuePair<DateTime, int> change in changesDone)
            {
                Console.WriteLine("Changed on: " + change.Key + ": " + change.Value);
            }

            Console.WriteLine();
        }
        public void RemoveAnnotation_Multiple()
        {
            JObject o = new JObject();

            o.AddAnnotation("A string!");
            o.AddAnnotation("A string 2!");
            o.AddAnnotation("A string 3!");

            o.RemoveAnnotations <string>();

            string s = o.Annotation <string>();

            Assert.AreEqual(null, s);

            o.AddAnnotation("A string 4!");

            s = o.Annotation <string>();
            Assert.AreEqual("A string 4!", s);

            Uri i = (Uri)o.Annotation(typeof(Uri));

            Assert.AreEqual(null, i);
        }
Example #14
0
        private void WriteToken(JSchema context, JsonWriter writer, JToken token)
        {
            if (token is JObject)
            {
                JObject o = (JObject)token;

                JSchemaAnnotation schemaAnnotation = o.Annotation <JSchemaAnnotation>();

                if (schemaAnnotation != null)
                {
                    ReferenceOrWriteSchema(context, schemaAnnotation.Schema, null);
                }
                else
                {
                    writer.WriteStartObject();

                    foreach (JProperty property in o.Properties())
                    {
                        writer.WritePropertyName(property.Name);

                        JToken value = property.Value;
                        if (value != null)
                        {
                            WriteToken(context, writer, value);
                        }
                        else
                        {
                            writer.WriteNull();
                        }
                    }

                    writer.WriteEndObject();
                }
            }
            else if (token is JArray)
            {
                JArray a = (JArray)token;

                writer.WriteStartArray();

                for (int i = 0; i < a.Count; i++)
                {
                    WriteToken(context, writer, a[i]);
                }

                writer.WriteEndArray();
            }
            else if (token is JConstructor)
            {
                JConstructor c = (JConstructor)token;

                writer.WriteStartConstructor(c.Name);

                foreach (JToken t in c.Children())
                {
                    WriteToken(context, writer, t);
                }

                writer.WriteEndConstructor();
            }
            else if (token is JValue)
            {
                token.WriteTo(writer);
            }
        }
        public static void Observe
        (
            this JObject target
            , Action
            <
                JObject
                , string
                , JObject
                , PropertyChangedEventArgs
                , JValue
                , JValue
            >
            onPropertyChangedProcessAction
        )
        {
            //target.Annotation<Action<string>>().
            // HashSet --> Dictionary
            target
            .AddAnnotation
            (
                new Dictionary <string, JObject>()
            );
            target
            .PropertyChanged +=
                (
                    (sender, args) =>
            {
                if (!target.Annotation <Dictionary <string, JObject> >().ContainsKey(args.PropertyName))
                {
                    JObject jDelegate = new JObject
                                        (
                        new JProperty
                        (
                            "methodName"
                            , onPropertyChangedProcessAction
                            .Method
                            .Name
                        )
                        , new JProperty
                        (
                            "isFlag"
                            ,
                            (
                                onPropertyChangedProcessAction
                                ==
                                null
                                                        ?
                                true
                                                        :
                                false
                            )
                        )
                                        );
                    target
                    .Annotation
                    <
                        Dictionary <string, JObject>
                    >()
                    .Add
                    (
                        args
                        .PropertyName
                        , jDelegate
                    );
                }
            }
                );

            Travel
            (
                target
                , null
                , (root, path, paropertyName, current) =>
            {
                JValue lastValue = null;
                current
                .PropertyChanging +=
                    (
                        (sender, args) =>
                {
                    if (sender is JObject jo)
                    {
                        lastValue = jo[args.PropertyName] as JValue;
                    }
                }
                    );
                JValue newValue = null;
                current
                .PropertyChanged +=
                    (
                        (sender, args) =>
                {
                    if (sender is JObject jo)
                    {
                        newValue = jo[args.PropertyName] as JValue;
                    }
                    onPropertyChangedProcessAction
                    (
                        root
                        , path
                        , current
                        , args
                        , lastValue
                        , newValue
                    );
                }
                    );
            }
            );
        }