<commit_before>﻿/*
 * Copyright © 2016 - 2017 EDDiscovery development team
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
 * file except in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under
 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
 * ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 * 
 * EDDiscovery is not affiliated with Frontier Developments plc.
 */
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text.RegularExpressions;

public static class JSONObjectExtensions
{
    static public bool Empty(this JToken token)
    {
        return (token == null) ||
                (token.Type == JTokenType.Array && !token.HasValues) ||
                (token.Type == JTokenType.Object && !token.HasValues) ||
                (token.Type == JTokenType.String && token.ToString() == String.Empty) ||
                (token.Type == JTokenType.Null);
    }

    static public string StrNull(this JToken jToken)
    {
        if (jToken.Empty())
            return null;
        try
        {
            return jToken.Value<string>();
        }
        catch { return null; }
    }

    static public string Str(this JToken jToken, string def = "")
    {
        if (jToken.Empty())
            return def;
        try
        {
            return jToken.Value<string>();
        }
        catch { return def; }
    }

    static public int Int(this JToken jToken, int def = 0)
    {
        int? f = jToken.IntNull();
        return (f != null) ? f.Value : def;
    }

    static public int? IntNull(this JToken jToken)
    {
        if (jToken.Empty())
            return null;
        try
        {
            return jToken.Value<int>();
        }
        catch { return null; }
    }

    static public long Long(this JToken jToken, long def = 0)
    {
        long? f = jToken.LongNull();
        return (f != null) ? f.Value : def;
    }<commit_msg>Defend Str() against string being null<commit_after>﻿/*
 * Copyright © 2016 - 2017 EDDiscovery development team
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
 * file except in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under
 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
 * ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 * 
 * EDDiscovery is not affiliated with Frontier Developments plc.
 */
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text.RegularExpressions;

public static class JSONObjectExtensions
{
    static public bool Empty(this JToken token)
    {
        return (token == null) ||
                (token.Type == JTokenType.Array && !token.HasValues) ||
                (token.Type == JTokenType.Object && !token.HasValues) ||
                (token.Type == JTokenType.String && token.ToString() == String.Empty) ||
                (token.Type == JTokenType.Null);
    }

    static public string StrNull(this JToken jToken)
    {
        if (jToken.Empty())
            return null;
        try
        {
            return jToken.Value<string>();
        }
        catch { return null; }
    }

    static public string Str(this JToken jToken, string def = "")
    {
        if (jToken.Empty())
            return def;
        try
        {
            string s = jToken.Value<string>();      // defend against json having a null
            return s != null ? s : def;
        }
        catch { return def; }
    }

    static public int Int(this JToken jToken, int def = 0)
    {
        int? f = jToken.IntNull();
        return (f != null) ? f.Value : def;
    }

    static public int? IntNull(this JToken jToken)
    {
        if (jToken.Empty())
            return null;
        try
        {
            return jToken.Value<int>();
        }
        catch { return null; }
    }

    static public long Long(this JToken jToken, long def = 0)
    {
        long? f = jToken.LongNull();
        return (f != null) ? f.Value : def;
    }
{'fraction_non_alphanumeric': 0.10010741138560687, 'fraction_numerical': 0.006015037593984963, 'mean_word_length': 2.1630434782608696, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7542380', 'n_tokens_mistral': 1305, 'n_tokens_neox': 1255, 'n_words': 447}