<commit_before>//
//  BasicUsage.playground
//  MetaSerialization
//
//  Copyright 2018 cherrywoods
//
//  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.
//

import Cocoa
import MetaSerialization

/*
 
 This playground contains a verry basic example
 of the usage of MetaSerialization
 
 A simple Translator will be created,
 that converts (certain) swift objects
 to some kind of simple json format.
 
 For that Translator a Serialization instance will be created and used.
 
 */

// MARK: - encoding to a reduced JSON format

func convertToJSON(_ value: Meta) -> String {
    
    // String and Int are the two supported types passed to PrimitivesEnumTranslator
    
    if let string = value as? String {
        
        return "\"\(string)\""
        
    } else if let int = value as? Int {
        
        return "\(int)"
        
    }
    
    // both Arrays and Dictionarys already contain encoded values
    
    // Array and Dictionary always need
    // to be supported when using PrimitivesEnumTranslator
    
    if let array = value as? Array<String> {
        
        var json = "[ \n"
        for element in array {
            json += element + "\n"
        }
        return json + "]"
        
    }
    
    if let dictionary = value as? Dictionary<String, String> {
        
        var json = "{ \n"
        for (key, val) in dictionary {
            json += "\"\(key)\": \(val)\n"
        }
        return json + "}"
        
    }
    
    return ""
    
}

func convertToSwift(_ json: String) -> Meta {
    var json = json
    
    switch json.first! {
    case "\"":
        // " at the beginning -> this is a string
        // remove the two "s
        // ( note that it is realy important to
        // return Strings and not Substrings,
        // because PrimitivesEnumTranslator can't handle Substrings )
        return String(json.dropFirst().dropLast())
    case "[":
        // [ at the beginning -> array
        // trim of [\n and \n]
        json = String( json.dropFirst(2).dropLast(2) )
        var array = [Any]()
        for element in json.split(separator: "\n") {
            array.append( convertToSwift(String(element)) )
        }
        return array
    case "{":
        // { -> dictionary
        // trim {\n and \n}
        json = String( json.dropFirst(2).dropLast(2) )
        var dictionary = [String: Any]()
        for line in json.split(separator: "\n") {
            let keyAndValue = line.split(separator: ":")
            // drop " and "
            let key = String( keyAndValue[0].dropFirst().dropLast() )
            // remove the blank before the values
            let val = convertToSwift(String( keyAndValue[1].dropFirst() ))
            dictionary[key] = val
        }
        return dictionary
    default:
        // this should be a number
        return Int(json)!
    }<commit_msg>Fix BasicUsage.playground<commit_after>//
//  BasicUsage.playground
//  MetaSerialization
//
//  Copyright 2018 cherrywoods
//
//  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.
//

import Cocoa
import MetaSerialization

/*
 
 This playground contains a verry basic example
 of the usage of MetaSerialization
 
 A simple Translator will be created,
 that converts (certain) swift objects
 to some kind of simple json format.
 
 For that Translator a Serialization instance will be created and used.
 
 */

// MARK: - encoding to a reduced JSON format

func convertToJSON(_ value: Meta) -> String {
    
    // String and Int are the two supported types passed to PrimitivesEnumTranslator
    
    if let string = value as? String {
        
        return "\"\(string)\""
        
    } else if let int = value as? Int {
        
        return "\(int)"
        
    }
    
    // both Arrays and Dictionarys already contain encoded values
    
    // Array and Dictionary always need
    // to be supported when using PrimitivesEnumTranslator
    
    if let array = value as? Array<Meta> {
        
        var json = "[ \n"
        for element in array {
            json += convertToJSON(element) + "\n"
        }
        return json + "]"
        
    }
    
    if let dictionary = value as? Dictionary<String, Meta> {
        
        var json = "{ \n"
        for (key, val) in dictionary {
            json += "\"\(key)\": \(convertToJSON(val))\n"
        }
        return json + "}"
        
    }
    
    return ""
    
}

func convertToSwift(_ json: String) -> Meta {
    var json = json
    
    switch json.first! {
    case "\"":
        // " at the beginning -> this is a string
        // remove the two "s
        // ( note that it is realy important to
        // return Strings and not Substrings,
        // because PrimitivesEnumTranslator can't handle Substrings )
        return String(json.dropFirst().dropLast())
    case "[":
        // [ at the beginning -> array
        // trim of [\n and \n]
        json = String( json.dropFirst(2).dropLast(2) )
        var array = [Meta]()
        for element in json.split(separator: "\n") {
            array.append( convertToSwift(String(element)) )
        }
        return array
    case "{":
        // { -> dictionary
        // trim {\n and \n}
        json = String( json.dropFirst(2).dropLast(2) )
        var dictionary = [String: Meta]()
        for line in json.split(separator: "\n") {
            let keyAndValue = line.split(separator: ":")
            // drop " and "
            let key = String( keyAndValue[0].dropFirst().dropLast() )
            // remove the blank before the values
            let val = convertToSwift(String( keyAndValue[1].dropFirst() ))
            dictionary[key] = val
        }
        return dictionary
    default:
        // this should be a number
        return Int(json)!
    }
{'fraction_non_alphanumeric': 0.10292380389840519, 'fraction_numerical': 0.004134672179562906, 'mean_word_length': 1.91562634524322, 'pattern_counts': {'":': 10, '<': 7, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3391188', 'n_tokens_mistral': 1874, 'n_tokens_neox': 1811, 'n_words': 685}