<commit_before>//
//  GlyphDescriptor.swift
//  CesiumKit
//
//  Created by <NAME> on 22/02/2016.
//  Copyright © 2016 Test Toast. All rights reserved.
//

import Foundation
import CoreGraphics

struct GlyphDescriptor: JSONEncodable {
    
    var glyphIndex: CGGlyph
    
    var topLeftTexCoord: CGPoint
    
    var bottomRightTexCoord: CGPoint
    
    init (glyphIndex: CGGlyph, topLeftTexCoord: CGPoint, bottomRightTexCoord: CGPoint) {
        self.glyphIndex = glyphIndex
        self.topLeftTexCoord = topLeftTexCoord
        self.bottomRightTexCoord = bottomRightTexCoord
    }
    
    init (json: JSONObject) throws {
        self.glyphIndex = try UInt16(json.getInt("glyphIndex"))
        self.topLeftTexCoord = try CGPoint(json: json.getObject("topLeftTexCoord"))
        self.bottomRightTexCoord = try CGPoint(json: json.getObject("bottomRightTexCoord"))
    }
    
    func toJSON() -> JSONObject {
        let object = JSONObject([
            "glyphIndex": JSON(integerLiteral: Int64(glyphIndex)),
            "topLeftTexCoord": JSON.Object(topLeftTexCoord.toJSON()),
            "bottomRightTexCoord": JSON.Object(bottomRightTexCoord.toJSON())
        ])
        return object
    }
    /*- (instancetype)initWithCoder:(NSCoder *)aDecoder
     {
     if ((self = [super init]))
     {
     _glyphIndex = [aDecoder decodeIntForKey:MBEGlyphIndexKey];
     _topLeftTexCoord.x = [aDecoder decodeFloatForKey:MBELeftTexCoordKey];
     _topLeftTexCoord.y = [aDecoder decodeFloatForKey:MBETopTexCoordKey];
     _bottomRightTexCoord.x = [aDecoder decodeFloatForKey:MBERightTexCoordKey];
     _bottomRightTexCoord.y = [aDecoder decodeFloatForKey:MBEBottomTexCoordKey];
     }
     
     return self;
     }
     
     - (void)encodeWithCoder:(NSCoder *)aCoder
     {
     [aCoder encodeInt:self.glyphIndex forKey:MBEGlyphIndexKey];
     [aCoder encodeFloat:self.topLeftTexCoord.x forKey:MBELeftTexCoordKey];
     [aCoder encodeFloat:self.topLeftTexCoord.y forKey:MBETopTexCoordKey];
     [aCoder encodeFloat:self.bottomRightTexCoord.x forKey:MBERightTexCoordKey];
     [aCoder encodeFloat:self.bottomRightTexCoord.y forKey:MBEBottomTexCoordKey];
     }*/
}

extension CGPoint: JSONEncodable {
    
    init (json: JSONObject) throws {
        self.x = try CGFloat(json.getDouble("x"))
        self.y = try CGFloat(json.getDouble("y"))
    }
    
    func toJSON() -> JSONObject {
        let object = JSONObject([
            "x": JSON(floatLiteral: Double(x)),
            "y": JSON(floatLiteral: Double(y))
            ])
        return object
    }
}<commit_msg>Improve JSON conversion<commit_after>//
//  GlyphDescriptor.swift
//  CesiumKit
//
//  Created by <NAME> on 22/02/2016.
//  Copyright © 2016 Test Toast. All rights reserved.
//

import Foundation
import CoreGraphics

private let GlyphIndexKey = "glyphIndex"
private let TopLeftTexCoordKey = "topLeftTexCoord"
private let BottomRightTexCoordKey = "bottomRightTexCoord"

struct GlyphDescriptor: JSONEncodable {
    
    var glyphIndex: CGGlyph
    
    var topLeftTexCoord: CGPoint
    
    var bottomRightTexCoord: CGPoint
    
    init (glyphIndex: CGGlyph, topLeftTexCoord: CGPoint, bottomRightTexCoord: CGPoint) {
        self.glyphIndex = glyphIndex
        self.topLeftTexCoord = topLeftTexCoord
        self.bottomRightTexCoord = bottomRightTexCoord
    }
    
    init (fromJSON json: JSON) throws {
        self.glyphIndex = try UInt16(json.getInt(GlyphIndexKey))
        self.topLeftTexCoord = try CGPoint(fromJSON: JSON.Object(json.getObject(TopLeftTexCoordKey)))
        self.bottomRightTexCoord = try CGPoint(fromJSON: JSON.Object(json.getObject(BottomRightTexCoordKey)))
    }
    
    func toJSON() -> JSON {
        let json = JSON.Object(JSONObject([
            GlyphIndexKey: JSON(integerLiteral: Int64(glyphIndex)),
            TopLeftTexCoordKey: topLeftTexCoord.toJSON(),
            BottomRightTexCoordKey: bottomRightTexCoord.toJSON()
        ]))
        return json
    }
    
}

extension CGPoint: JSONEncodable {
    
    init (fromJSON json: JSON) throws {
        self.x = try CGFloat(json.getDouble("x"))
        self.y = try CGFloat(json.getDouble("y"))
    }
    
    func toJSON() -> JSON {
        let json = JSON.Object(JSONObject([
            "x": JSON(floatLiteral: Double(x)),
            "y": JSON(floatLiteral: Double(y))
            ]))
        return json
    }
}
{'fraction_non_alphanumeric': 0.10189627598811972, 'fraction_numerical': 0.00731094356865433, 'mean_word_length': 2.9512635379061374, 'pattern_counts': {'":': 7, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '4028333', 'n_tokens_mistral': 1407, 'n_tokens_neox': 1346, 'n_words': 260}