1 using System.Collections.Generic;
2 using System.Globalization;
3 using Newtonsoft.Json.Utilities;
4 
5 namespace Newtonsoft.Json.Linq.JsonPath
6 {
7     internal class FieldFilter : PathFilter
8     {
9         public string Name { get; set; }
10 
ExecuteFilter(IEnumerable<JToken> current, bool errorWhenNoMatch)11         public override IEnumerable<JToken> ExecuteFilter(IEnumerable<JToken> current, bool errorWhenNoMatch)
12         {
13             foreach (JToken t in current)
14             {
15                 JObject o = t as JObject;
16                 if (o != null)
17                 {
18                     if (Name != null)
19                     {
20                         JToken v = o[Name];
21 
22                         if (v != null)
23                         {
24                             yield return v;
25                         }
26                         else if (errorWhenNoMatch)
27                         {
28                             throw new JsonException("Property '{0}' does not exist on JObject.".FormatWith(CultureInfo.InvariantCulture, Name));
29                         }
30                     }
31                     else
32                     {
33                         foreach (KeyValuePair<string, JToken> p in o)
34                         {
35                             yield return p.Value;
36                         }
37                     }
38                 }
39                 else
40                 {
41                     if (errorWhenNoMatch)
42                     {
43                         throw new JsonException("Property '{0}' not valid on {1}.".FormatWith(CultureInfo.InvariantCulture, Name ?? "*", t.GetType().Name));
44                     }
45                 }
46             }
47         }
48     }
49 }