Advertisement
Guest User

Untitled

a guest
May 9th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 7.10 KB | None | 0 0
  1. open System
  2. open System.IO
  3. open System.Net
  4. open System.Text
  5. open System.Collections.Specialized
  6.  
  7. // почтовый адрес
  8. let email = "natakir75@gmail.com"
  9.  
  10.  
  11.  
  12. type Types =
  13.   | OpenBrace | CloseBrace // { }
  14.   | OpenBracket | CloseBracket // [ ]
  15.   | Colon | Comma
  16.   | String of string
  17.   | Number of int
  18.   | Boolean of bool
  19.   | Null
  20.  
  21.  
  22. let devideJSON source =
  23.   let rec token acc = function
  24.     | (x :: _) as t when List.exists ((=)x) [':'; ','; ')'; ']'] -> acc, t
  25.     | w :: t when Char.IsWhiteSpace(w) -> acc, t
  26.     | [] -> acc, []
  27.     | c :: t -> token (acc + (c.ToString())) t
  28.  
  29.   let rec parseString acc = function
  30.     | '\\' :: '"' :: t -> parseString (acc + "\"") t
  31.     | '\\' :: 'n' :: t -> parseString (acc + "\n") t
  32.     | '"' :: t -> acc, t
  33.     | c :: t -> parseString (acc + c.ToString()) t
  34.     | _ -> failwith "Wrong JSON structure!"
  35.  
  36.   let rec tokenize acc = function
  37.     | w :: t when Char.IsWhiteSpace(w) -> tokenize acc t
  38.     | '{' :: t -> tokenize (OpenBrace :: acc) t
  39.     | '}' :: t -> tokenize (CloseBrace :: acc) t
  40.     | '[' :: t -> tokenize (OpenBracket :: acc) t
  41.     | ']' :: t -> tokenize (CloseBracket :: acc) t
  42.     | ':' :: t -> tokenize (Colon :: acc) t
  43.     | ',' :: t -> tokenize (Comma :: acc) t
  44.     // start of string
  45.     | '"' :: t ->
  46.       let s, t' = parseString "" t
  47.      tokenize (String s :: acc) t'    
  48.     | 'n' :: 'u' :: 'l' :: 'l' :: t -> tokenize (Null :: acc) t
  49.     | 't' :: 'r' :: 'u' :: 'e' :: t -> tokenize (Boolean true :: acc) t
  50.     | 'f' :: 'a' :: 'l' :: 's' :: 'e' :: t -> tokenize (Boolean false :: acc) t
  51.     // numbers remained
  52.     | d :: t ->
  53.       let n, t' = token (d.ToString()) t
  54.      tokenize (Number (try Convert.ToInt32 n with e -> 0)  :: acc) t'
  55.     | [] -> List.rev acc
  56.     | _ -> failwith "Wrong JSON structure!"
  57.   tokenize [] source
  58.  
  59. type JSON =
  60.   | Object of (string * JSON) list
  61.   | Array of JSON list
  62.   | Number of int
  63.   | String of string
  64.   | Boolean of bool
  65.   | Null
  66. let rec parseJSON json =
  67.   let rec parse json =
  68.  
  69.     let rec parseArray list = function
  70.       | CloseBracket :: t -> (Array (List.rev list)), t
  71.       | Comma :: t -> parseArray list t
  72.       | ob ->
  73.         let a, t = parse ob
  74.         parseArray (a :: list) t
  75.  
  76.     let rec parseObject list = function
  77.       | CloseBrace :: t -> (Object (List.rev list)), t
  78.       | Comma :: t -> parseObject list t
  79.       | Types.String s :: Colon :: t ->
  80.         let a, t = parse t
  81.         parseObject ((s, a) :: list) t
  82.       | _ -> failwith "Wrong JSON structure!"
  83.    
  84.     match json with
  85.       | OpenBrace :: t -> parseObject [] t
  86.       | OpenBracket :: t -> parseArray [] t
  87.      
  88.       | Types.String s :: t -> JSON.String s, t
  89.       | Types.Number s :: t -> JSON.Number s, t
  90.       | Types.Boolean s :: t -> JSON.Boolean s, t
  91.       | Types.Null :: t -> JSON.Null, t
  92.       | _ -> failwith "Wrong JSON structure!"
  93.   match parse json with
  94.     | res, [] -> res
  95.     | _ -> failwith "Wrong JSON structure"
  96.  
  97.  
  98. let explode (s:string) =
  99.   [for c in s -> c]
  100.  
  101. let are_equal (tree1 : JSON) (tree2 : JSON) : bool =
  102.     let rec cmppair (tr1 : string*JSON) (tr2 : string*JSON) : bool =
  103.       if fst tr1 <> fst tr2 then false else recf (snd tr1) (snd tr2)
  104.  
  105.     and recf tr1 tr2 =
  106.         match tr1 with
  107.         | Number n ->
  108.           match tr2 with
  109.           | Number comp -> n = comp
  110.           | _ -> false
  111.         | Boolean n ->
  112.           match tr2 with
  113.           | Boolean comp -> n = comp
  114.           | _ -> false
  115.         | String n ->
  116.           match tr2 with
  117.           | String comp ->
  118.              n = comp
  119.           | _ -> false
  120.         | Null ->
  121.           match tr2 with
  122.           | Null -> true
  123.           | _ -> false
  124.         | Array n ->
  125.           match tr2 with
  126.           | Array comp ->
  127.             if n.Length = comp.Length then
  128.               List.forall2 recf n comp
  129.             else false
  130.           | _ -> false
  131.         | Object n ->
  132.           match tr2 with
  133.           | Object comp ->
  134.             if n.Length = comp.Length then
  135.               List.forall2 cmppair n comp
  136.             else false
  137.           | _ -> false
  138.     let res = recf tree1 tree2
  139.     res
  140.  
  141. let firstTest = """
  142. {
  143.  "a": 1
  144.  "n": {
  145.    "c": [{
  146.      "18":27
  147.      "2": {
  148.        "2": 10
  149.      }
  150.    },2,4]
  151.  }
  152. }
  153. """
  154.  
  155.  
  156. let secondTest = """
  157. {
  158.  "a": 1
  159.  "n": {
  160.    "c": [{
  161.      "18":27
  162.      "2": {
  163.        "2": 10
  164.      }
  165.    },2,3]
  166.  }
  167. }
  168. """
  169.  
  170. let ans1 = are_equal (firstTest |> explode |> devideJSON |> parseJSON) (firstTest |> explode |> devideJSON |> parseJSON)
  171. let ans2 = are_equal (firstTest |> explode |> devideJSON |> parseJSON) (secondTest |> explode |> devideJSON |> parseJSON)
  172. let addWhiteSpaces str =
  173.         let s = explode str
  174.         "  " + (Seq.fold (fun acc x -> acc + x) "" (Seq.map (fun c -> c.ToString() + (if c = '\n' then "  " else "")) s))
  175.  
  176. let rec stringify = function
  177.         | Number n -> n.ToString()
  178.         | String s -> "\"" + s + "\""
  179.         | Boolean b -> if b then "true" else "false"
  180.         | Null -> "null"
  181.         | Array a -> "[" + (Seq.fold (fun acc x ->
  182.             (acc + (if acc.Equals("") then "" else ",") + (stringify x))) "" a) + "]"
  183.         | Object list ->
  184.             "{\n" + (addWhiteSpaces (Seq.fold (fun acc (x,y) -> (acc + "\"" + x + "\": " + (stringify y)) + "\n") "" list)) + "\n}"
  185.  
  186. let generate random =
  187.     let randomString random =
  188.         let size = (Seq.item 1 random) + 1
  189.         let s = Seq.init size (fun _ -> (Seq.item 1 random) % 26)
  190.         Seq.fold (fun acc x -> acc + ('a' + char(x)).ToString()) "" s
  191.     let rec generate' random depth =
  192.        match Seq.item 1 random with
  193.        | 0 -> JSON.Null
  194.        | 1 -> JSON.Number (Seq.item 1 random)
  195.        | 2 -> JSON.String (randomString random)
  196.        | 3 -> JSON.Boolean (if (Seq.item 1 random) % 2 = 0 then true else false)
  197.        | _ when depth > 3 -> JSON.Null // not to grow deep
  198.        | x when x % 2 = 0 ->
  199.            let size = Seq.item 1 random
  200.            let s = Seq.init size (fun i -> i)
  201.            JSON.Array [for i in s -> generate' random (depth + 1)]
  202.         | _ ->
  203.             let size = (Seq.item 1 random) + 1
  204.             let s = Seq.init size (fun i -> i)
  205.             let values = [for i in s -> generate' random (depth + 1)]
  206.            let mutable st = new Set<string>([])
  207.            while (Set.count st < size) do
  208.                st <- Set.add (randomString random) st
  209.            let strings = Set.toList st
  210.            JSON.Object (List.map2 (fun x y -> (x, y)) strings values)
  211.    generate' random 0
  212.  
  213. let rand maxValue =
  214.     let rnd = new Random(1337)
  215.     Seq.initInfinite(fun _ -> rnd.Next(maxValue))
  216.  
  217.  
  218. //testing the generator and stringifier
  219. let a = rand 10
  220. let t = generate a;;
  221. stringify t;;
  222.  
  223. let main () =
  224.   let values = new NameValueCollection()
  225.   values.Add("email", email)
  226.   values.Add("content", File.ReadAllText(__SOURCE_DIRECTORY__ + @"/" + __SOURCE_FILE__))
  227.  
  228.   let client = new WebClient()
  229.   let response = client.UploadValues(new Uri("http://91.239.142.110:13666/lab2"), values)
  230.   let responseString = Text.Encoding.Default.GetString(response)
  231.  
  232.   printf "%A\n" responseString
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement